[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How can I use ASP.NET/VB.NET to save a CSV File to Client Machine?

Posted on 2005-04-25
8
Medium Priority
?
1,066 Views
Last Modified: 2008-02-01
I thought this would be easier but I've been trying to figure it out for a while now. From a form, I want my site users to select a file path on their machine, when the form is submitted, the .csv file is generated based on their form input and saved in the location they chose. Sounds easy enough, but I'm having problems. I've got everything set up but the file logic.

So far I've got this in the .vb file.
----
            Dim objStreamWriter as StreamWriter
            
            objStreamWriter = File.CreateText(MapPath(" I want to put the file path the user chose here I think  "))
            objStreamWriter.WriteLine(strDynamicCSV)
            objStreamWriter.Close
--------

in the aspx page I have this to prompt the user.
-----------
<form runat="server" id="login" enctype="multipart/form-data">
               <input id="uploadedFile" type="file" runat="server">
</form>
--------

After many google searches I still can't figure out how to extract the file name the user chose from the type="file" input box.

If the streamwriter object isn't the correct way to do this, then please let me know another way. But if this will work if I can get the file path then it looks fine.

Thank you for your time.

Scott

0
Comment
Question by:sneidig
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 13864011
http://www.developer.com/net/vb/article.php/3097661 should help you ...

the problem you are having is that you are trying to access the file path ...

you want to access the FileField.PostedFile file.

Cheers,

Greg
0
 
LVL 14

Expert Comment

by:puranik_p
ID: 13864174
@Greg,
the link you've posted is about file upload which I don't see anyway related to this Q.

@Scott,
>I want my site users to select a file path on their machine
What you are trying to achive here is access clients harddisk and save a file there from a web page.
Now,
You can not do it with server side script as in the code you have posted.
You can do it with client side script. - For this you need to send the data to be put in the file in plain HTML hidden field or HTML table if you don't mind the user viewing it.
Here is how to it.
http://www.xpounded.netfirms.com/xShellToolJS.html
0
 
LVL 14

Expert Comment

by:puranik_p
ID: 13864180
Forgot to mention above, you need to alter the process, first generate the content of the file with the form data and then ask user for location to save.

Pura
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 26

Accepted Solution

by:
Rejojohny earned 1200 total points
ID: 13864487
>> From a form, I want my site users to select a file path on their machine, when the form is submitted, the .csv file is generated based on their form input

<input type=file is used for file uploads and not for selecting paths at the client side .. anyway storing a file on the client from the server is also not possible

Instead of this approach (i.e. first selecting the path), u could first ask the user to input data and then u could display the data in csv format to them .. based on the settings on their machine, the csv file would be shown in the browser or a open/save dialog box would popup using which they could save the file in any path they find suitable ..

for this have a look at the following link
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q308247
http://support.microsoft.com/default.aspx?scid=kb;EN-US;199841
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q319180

0
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 800 total points
ID: 13866808
Sorry I must have been confused based on the fact that your code was looking like it was trying to upload a file ...

to export a csv to a client machine ... just change your response mime type to excel and write cvs directly to reponse ... it will depending on their setup either bring it up in excel where they can save it or it will prompt them immediately to save it. For security reasons you are not allowed to write directly to their hard drive.
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 13867650

You can't upload data to a client machine.  That violates every web browser's functionality.  You *can* make the file available for download.  But then the client has to download the file manually.

The last option is to create an ActiveX dll that the client downloads and installs.  Your program can then access the activeX dll on the client machine and it will do the work of download the file for the client.

Note:  Opera, and Firefox do NOT support ActiveX
0
 

Author Comment

by:sneidig
ID: 13868531
hi experts, thanks for replies. i will be looking at these in a few hours and will post responses by afternoon.
0
 

Author Comment

by:sneidig
ID: 13880006
RejoJohny said,
"Instead of this approach (i.e. first selecting the path), u could first ask the user to input data and then u could display the data in csv format to them .. based on the settings on their machine, the csv file would be shown in the browser or a open/save dialog box would popup using which they could save the file in any path they find suitable .."

that is pretty much what i ended up doing. i didn't use the links you added to msdn. i took variables entered into the starting page, passed them thru the url to another page, the .vb page takes the values out of the url and builds the csv file (into a variable value) and then prints it on the page with this code

      Page.Response.Clear()            
      Response.ContentType = "text/plain"
      Page.Response.AddHeader("content-disposition", "filename=whatever.csv")
      Page.Response.Output.Write(strCsvFile)

the .aspx page associated with that .vb file has no data in it at all except for the page directive and onload it will "display the data in csv format to them". then it's up to the user to choose save as from the browser if they're not prompted.

thanks, i'm going to give john and greg some points.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question