• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1188
  • Last Modified:

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

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
sneidig
Asked:
sneidig
  • 2
  • 2
  • 2
  • +2
2 Solutions
 
gregoryyoungCommented:
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
 
puranik_pCommented:
@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
 
puranik_pCommented:
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
RejojohnyCommented:
>> 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
 
gregoryyoungCommented:
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
 
_TAD_Commented:

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
 
sneidigAuthor Commented:
hi experts, thanks for replies. i will be looking at these in a few hours and will post responses by afternoon.
0
 
sneidigAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now