How create a file on users machine (ASP.NET)?

  • Thread starter Thread starter VB Programmer
  • Start date Start date
V

VB Programmer

I want my webform to create a file on the users machine. How can I do this?
I tried this but it doesn't work...

Dim strFileName As String = "C:\Temp\MyFile.ABC"
Dim sw As New StreamWriter(strFileName)
sw.WriteLine(lblStartRun.Text)
sw.Close()
Response.Redirect(strFileName)

The ABC file extension is associated with a custom application on the users
local machine.

The error is "This page cannot be displayed..."

Thanks,
Robert
 
Try putting a try catch around and see what your error is..........

--
Regards

John Timney (Microsoft ASP.NET MVP)
----------------------------------------------
<shameless_author_plug>
Professional .NET for Java Developers with C#
ISBN:1-861007-91-4
Professional Windows Forms
ISBN: 1861005547
Professional JSP 2nd Edition
ISBN: 1861004958
Professional JSP
ISBN: 1861003625
Beginning JSP Web Development
ISBN: 1861002092
</shameless_author_plug>
 
The problem is possible that that file path is not part of the web site and
will not be served by IIS
Try a path under the web directory eg c:\Inetpub\wwwroot\MyWeb\MyFiles\
or
write the file stream directly out to the browser without the overhead of
saving to disk first.
Set the file type ( doc, pdf , txt etc) in the header first.
A
 
I suspect you have a permissions problem. Perhaps the ASPNET user account
does not have write privilages to the temp folder you've specified.
One way to avoid such issues is to output the file directly to the user
without ever saving it to the server's hard drive.
Some code like this should do it:

Response.Clear();
Response.AddHeader("Content-Disposition","attachment;filename=myfile.abc");
Response.WriteFile("myfile.abc");

Here's more info:
http://msdn.microsoft.com/library/d...fsystemwebhttpresponseclasswritefiletopic.asp
 
VB Programmer said:
I want my webform to create a file on the users machine. How can I do this?
I tried this but it doesn't work...

Dim strFileName As String = "C:\Temp\MyFile.ABC"
Dim sw As New StreamWriter(strFileName)
sw.WriteLine(lblStartRun.Text)
sw.Close()
Response.Redirect(strFileName)

C:\Temp\MyFile.ABC is not a URL. What do you expect the user's browser to do
with this string?
 
Back
Top