I have a webform when use access that form on the Page Load event the following method calls for downloading. Here I used both Response.TransmitFile and Response.WriteFile to send the file to the client's browser to show Open/Save/Cancel dialog box but it
does not work.
My file is exist on some remote location and using the absolute path for downloading as mentioned below.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.ContentType = "video/mp4"
Response.Clear()
Response.AddHeader("content-disposition", "attachment")
Response.TransmitFile("http://localhost/file/Chaos.mp4")
Response.End()
End Sub
Kindly help me to resolve this issue. Also If I could detect the file has been downloaded from the server.
'Response.Write(sPath)
Dim strMappedPath As String = HttpContext.Current.Server.MapPath(sPath)
Response.Write(strMappedPath)
If File.Exists(strMappedPath) Then
Dim filedata As Byte()
Using fs As New FileStream(HttpContext.Current.Server.MapPath(sPath), FileMode.Open, FileAccess.Read)
Dim lngLen = fs.Length
ReDim filedata(CInt(lngLen - 1))
fs.Read(filedata, 0, CInt(lngLen))
End Using
With Response
.Clear()
.ContentType = _doc.ImageType
.Cache.SetCacheability(HttpCacheability.Private)
.Expires = -1
.Buffer = True
.AddHeader("Content-Disposition", String.Format("{0};FileName=""{1}""", "attachment", Path.GetFileName(strMappedPath)))
.BinaryWrite(filedata)
.End()
End With
Else
Response.Write(" Error downloading file.")
End If
End If
End If
You cannot use any transmit file methods in that fashion. The files must be local to the file system. You could either redirect them to the other website, or use the httpwebrequest/httpwebresponse classes to download and transmit the file yourself. The latter
is not a good option for large files since you would require a full download before you could transmit it to the client.
Don't forget to mark useful responses as Answer if they helped you towards a solution.
The thing is the file which I want to download is exist on some remote location for example
http://www.abc.com/folder/chaos.mp4 And the application from where the user is downloading the file is on some different location for example
http://www.xyz.com where the user has a list of files for downloading, when user clicks on the link to download the file should be download from the remote location like
http://www.abc.com/folder/chaos.mp4
I think you r forgetting to attach the file..just chk the code below..
string pdfPath = MapPath("mypdf.pdf");
Response.ContentType = "Application/pdf";
Response.AppendHeader("content-disposition", "attachment; filename=" + pdfPath );
Response.TransmitFile(pdfPath);
Response.End();
knowledgist
Member
86 Points
135 Posts
Send a file to Client's Browser for downloading using Absolute Path
Nov 17, 2012 02:32 PM|LINK
Hey All,
I have a webform when use access that form on the Page Load event the following method calls for downloading. Here I used both Response.TransmitFile and Response.WriteFile to send the file to the client's browser to show Open/Save/Cancel dialog box but it does not work.
My file is exist on some remote location and using the absolute path for downloading as mentioned below.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Response.ContentType = "video/mp4" Response.Clear() Response.AddHeader("content-disposition", "attachment") Response.TransmitFile("http://localhost/file/Chaos.mp4") Response.End() End SubKindly help me to resolve this issue. Also If I could detect the file has been downloaded from the server.
Thank you.
oned_gk
All-Star
31017 Points
6352 Posts
Re: Send a file to Client's Browser for downloading using Absolute Path
Nov 17, 2012 02:40 PM|LINK
Shailendra S...
Member
551 Points
145 Posts
Re: Send a file to Client's Browser for downloading using Absolute Path
Nov 17, 2012 03:16 PM|LINK
Private Sub getFile()
If DocumentID > 0 Then
Dim _doc As New DocumentRepository
_doc = DocumentsRepository.GetFile(DocumentID)
If _doc IsNot Nothing Then
Dim sPath As String
sPath = String.Format("/{0}/{1}{2}", spath2, _doc.FileName, _doc.FileExt)
'sPath = String.Format("/{0}/{1}{2}", _doc.FilePath, _doc.FileName, _doc.FileExt)
'Response.Write(sPath)
Dim strMappedPath As String = HttpContext.Current.Server.MapPath(sPath)
Response.Write(strMappedPath)
If File.Exists(strMappedPath) Then
Dim filedata As Byte()
Using fs As New FileStream(HttpContext.Current.Server.MapPath(sPath), FileMode.Open, FileAccess.Read)
Dim lngLen = fs.Length
ReDim filedata(CInt(lngLen - 1))
fs.Read(filedata, 0, CInt(lngLen))
End Using
With Response
.Clear()
.ContentType = _doc.ImageType
.Cache.SetCacheability(HttpCacheability.Private)
.Expires = -1
.Buffer = True
.AddHeader("Content-Disposition", String.Format("{0};FileName=""{1}""", "attachment", Path.GetFileName(strMappedPath)))
.BinaryWrite(filedata)
.End()
End With
Else
Response.Write(" Error downloading file.")
End If
End If
End If
End Sub
www.techaray.com
ramiramilu
All-Star
95275 Points
14072 Posts
Re: Send a file to Client's Browser for downloading using Absolute Path
Nov 17, 2012 03:43 PM|LINK
set ContentLength too - http://www.intstrings.com/ramivemula/asp-net/how-to-download-files-from-server-to-client-using-a-generic-handler/
Thanks,
JumpStart
markfitzme
Star
14323 Points
2217 Posts
Re: Send a file to Client's Browser for downloading using Absolute Path
Nov 17, 2012 05:08 PM|LINK
You cannot use any transmit file methods in that fashion. The files must be local to the file system. You could either redirect them to the other website, or use the httpwebrequest/httpwebresponse classes to download and transmit the file yourself. The latter is not a good option for large files since you would require a full download before you could transmit it to the client.
Ruchira
All-Star
42888 Points
7020 Posts
MVP
Re: Send a file to Client's Browser for downloading using Absolute Path
Nov 17, 2012 05:14 PM|LINK
Hello,
Try this
Response.Clear() Response.ContentType = "application/octet-stream" Response.AddHeader("Content-Disposition", "attachment;filename=Chaos.mp4") Response.Flush() Response.TransmitFile("http://localhost/file/Chaos.mp4") Response.End()
My Tech blog | My YouTube ChannelPlease 'Mark as Answer' if this post helps you.knowledgist
Member
86 Points
135 Posts
Re: Send a file to Client's Browser for downloading using Absolute Path
Nov 19, 2012 05:17 AM|LINK
The thing is the file which I want to download is exist on some remote location for example http://www.abc.com/folder/chaos.mp4 And the application from where the user is downloading the file is on some different location for example http://www.xyz.com where the user has a list of files for downloading, when user clicks on the link to download the file should be download from the remote location like http://www.abc.com/folder/chaos.mp4
Ruchira
All-Star
42888 Points
7020 Posts
MVP
Re: Send a file to Client's Browser for downloading using Absolute Path
Nov 19, 2012 05:47 AM|LINK
Oopss. In that case, Mark is correct. You have to use his solution.
My Tech blog | My YouTube ChannelPlease 'Mark as Answer' if this post helps you.manjuby
Participant
1131 Points
251 Posts
Re: Send a file to Client's Browser for downloading using Absolute Path
Nov 19, 2012 05:48 AM|LINK
knowledgist
Member
86 Points
135 Posts
Re: Send a file to Client's Browser for downloading using Absolute Path
Nov 19, 2012 07:06 AM|LINK
Ruchira, I didn't get you....