Allow User to download file...
I have a button click event on my page...
During this code execution, I may or maynot create a woof.txt file.
At the end of the button click, if Woof.txt exists, then I would like the user to see a Save As Diaglog box...
What's the 2 lines of code for this?
Woka
Re: Allow User to download file...
VB Code:
if System.IO.File.Exists(Server.MapPath("createdfiles/woof.txt")) Then
Dim TargetFile As New System.IO.FileInfo(Server.MapPath("createdfiles/woof.txt"))
' clear the current output content from the buffer
Response.Clear()
' add the header that specifies the default filename for the Download/
' SaveAs dialog
Response.AddHeader("Content-Disposition", "attachment; filename=" + _
TargetFile.Name)
' add the header that specifies the file size, so that the browser
' can show the download progress
Response.AddHeader("Content-Length", TargetFile.Length.ToString())
' specify that the response is a stream that cannot be read by the
' client and must be downloaded
Response.ContentType = "application/octet-stream"
' send the file stream to the client
Response.WriteFile(TargetFile.FullName)
' stop the execution of this page
Response.End()
End If
Re: Allow User to download file...
Hehe. Yea, just implemented that code you posted for dj4uk :D
It works...but...errr...but...Why on earth do I have to do that?
Why can't I just do something simple like...
VB Code:
Response.DownloadFile = "Woof.txt";
:(
Stupid .NET.
Thanks for your code. It works...if only I could work out what the application type needs to be for csv files.
Woof
Re: Allow User to download file...
It's not a .NET issue, it's web architecture. Although you could probably inherit and create your own method for that. ;)
The content type is application/csv or you can use application/vnd.ms-excel.
Re: Allow User to download file...
yea, I'm going to :D
Cheers for your code on this. :D
Woof
Re: Allow User to download file...
Re: Allow User to download file...
I would do this more inefficiently. Create woof.txt in a browsable directory,
if File.Exists("woof.txt") then
Response.Redirect("woof.txt");
end if
Then they can do a file/save.
(this is if you want an easier but less secure way to do it, as anyone can browse to that page)
Re: Allow User to download file...
The only problem with this, as it seems to me, is that it is not thread safe. It's my only guess why I can't get it to work. So here is what I have. A form that takes the users information, (name email,,,,) then a button to save and download a file. I have a sub that turns all of the fields and labels off, then turns on a thank you label. Then the sub which does the down load is called. The issue is the textboxes and labels don't turn off. If I skip the download sub works fine. Kind of got me here.
Protected Sub BtnSubmit_Click(sender As Object, e As EventArgs) Handles BtnSubmit.Click
Dim strBook As String
Dim intSuccess As Integer
If Request.QueryString("book") <> "" Then ' checks to see if someone has messed with query string,, not that anyone ever does that!
strBook = Request.QueryString("book").ToString 'This is the name of the ebook file that is going to be downloaded
intSuccess = SendEmail(0, strBook) ' This function stores the user data to a database, then sends them an email
If intSuccess.ToString = 1 Then ' a 1 here means the data was stored and the email was sent
DoDownLoad(strBook) ' almost word for word what is listed above
SuccessPage() 'Turns off many labels and test boxes, like this: EnterLastName.Visible = False
Else
FailPage("Fault, Sorry please try again") ' This will just trigger an error message as shown
End If
Else
FailPage("No File Selected")
End If
End Sub
Re: Allow User to download file...
Quote:
I have a sub that turns all of the fields and labels off, then turns on a thank you label. Then the sub which does the down load is called.
And yet... your code seems to suggest the opposite:
Code:
DoDownLoad(strBook) ' almost word for word what is listed above
SuccessPage() 'Turns off many labels and test boxes, like this: EnterLastName.Visible = False
somethign to keep in mind - UI updates are low priority to the OS. What you may want/need to do is after setting the items to invisible, invalidate the form, which should force a repainting of the form... THEN call your download method.
-tg
Re: Allow User to download file...
So,,, Mr. nooob here, but I can't seem to see how to do the invalidate method. I am / was sure it's a UI issue, I was thinking it was a thread thing, though not doing anything with threads on this.
But to clarify the "SuccessPage()" and "FailedPage" subs. this is success or failure of the database write. If the database write goes ok, that is "Success" and all controls should be turned off (EnterLastName.Visible = False (textbox) 'this is part of the SuccessPage() sub)
This is really driving me nuts! I have never had anything like this before. Any help would be great,,, thanks.