Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hey everyone. I am trying to get a file to download to browser or give the option of saving to disk when selected by a user from a webpage. My issue is very simple but I cannot seem to figure out how to solve it. I have a page that is called when appropriate and passed in is a filename to download. The directory is irrelevant at this point based on how I have set up the next phase.

 

The called page consumes a webservice which takes the passed in filename and a password, and then returns in a string the specific path to the file on the server. In this manner even the webpage is independent of the location of the files, and more obscurity is obtained for security purposes. Anyway, after all of this, my issue is that the code I'm using to try to download the file seems to be looking for the file on the client machine, and not on the server. I continue to get the 'File not found' exception, even though the file does exist on the server. If I mimic the path on the client machine, I of course do not get the error, which is how I've determined that I'm not looking at the correct machine.

 

Is there a specific setting or chunk of code that I might be missing that would make sure the path for file download is server-side and not client-side? Here is my code:

 

Try
           Dim myWFS As New WebFileShare.WebFileShare
           Dim source As String = Request("source")
           If IsNothing(source) Then
               Throw New Exception("ERROR: No filename passed in, cannot procede!")
           End If
           Dim filepath As String = myWFS.GetFilePath(source, myPASSWD)

           If Not filepath Is Nothing Then
               If File.Exists(filepath) Then
                   Response.Clear()
                   Response.ContentType = "application/octet-stream"
                   Response.AddHeader("Content-Disposition", _
                     "attachment; filename=""" & source & """")
                   Response.Flush()
                   Response.WriteFile(filepath)
               End If
           End If
       Catch ex As Exception
           If Trace.IsEnabled Then
               Trace.Write("AssociatedFileDownload", ex.ToString())
           End If
       Finally
           Response.Write("<script language=""javascript"">window.close();</script>")
       End Try

 

Thanks, Brian

Posted

Allright, chalk this one up to working on Saturday when it's really nice out...but, I have built a work-around, in which my web service returns the valid URL path (using a virtual directory) instead of server filepath to the file, so instead of trying to stream it, I just launch a new webpage and have that page redirect to the URL for the file which accomplishes the download/save option. Not the exact way I wanted it to work, but it is a solution. I was hoping to stream the output, maybe another time I will figure all that out.

 

Thanks,

Brian

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...