Results 1 to 10 of 10

Thread: Allow User to download file...

  1. #1

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Allow User to download file...

    VB Code:
    1. if System.IO.File.Exists(Server.MapPath("createdfiles/woof.txt")) Then
    2.  
    3.         Dim TargetFile As New System.IO.FileInfo(Server.MapPath("createdfiles/woof.txt"))
    4.  
    5.         ' clear the current output content from the buffer
    6.         Response.Clear()
    7.         ' add the header that specifies the default filename for the Download/
    8.         ' SaveAs dialog
    9.         Response.AddHeader("Content-Disposition", "attachment; filename=" + _
    10.             TargetFile.Name)
    11.         ' add the header that specifies the file size, so that the browser
    12.         ' can show the download progress
    13.         Response.AddHeader("Content-Length", TargetFile.Length.ToString())
    14.         ' specify that the response is a stream that cannot be read by the
    15.         ' client and must be downloaded
    16.         Response.ContentType = "application/octet-stream"
    17.         ' send the file stream to the client
    18.         Response.WriteFile(TargetFile.FullName)
    19.         ' stop the execution of this page
    20.         Response.End()
    21.  
    22. End If

  3. #3

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Allow User to download file...

    Hehe. Yea, just implemented that code you posted for dj4uk
    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:
    1. 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

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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.

  5. #5

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Allow User to download file...

    Woof.

  7. #7
    Fanatic Member
    Join Date
    Jun 2005
    Posts
    625

    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)

  8. #8
    New Member
    Join Date
    Feb 2014
    Posts
    2

    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

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Allow User to download 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.
    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10
    New Member
    Join Date
    Feb 2014
    Posts
    2

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width