Answered by:
Download File (image) from ASP always get corrupt file

Question
-
User1228272764 posted
here is my upload code :
Dim filereceived As String = FileUpload1.PostedFile.FileName Dim filename As String = Path.GetFileName(filereceived) Dim EditDataStafffoto As String 'get data staff id izin Call GetData_IdIzinPengajuan("select top 1 IZIN_ID from DATA_IZIN_BODY order by izin_id desc") EditDataStafffoto = "UPDATE data_izin_body SET IZIN_FILEDETAIL='" & filename & "' WHERE IZIN_ID='" & idizin_pertama & "'" Call UpdateData_Server(EditDataStafffoto, "") Dim Fileuploadpath As String = "D:/Herlambang/September-Ongoing - Revisi Input Data Izin (Table)/LiveMUGENASP/MugenASPHRISO_10_22_018/img/imgstaff/" FileUpload1.PostedFile.SaveAs(Path.Combine(fileuploadpath, filename))
here is my download code:
Call GetData_DetaiIzinStaff(" Select * FROM DATA_IZIN_BODY WHERE IZIN_ID = '" & TxtDetailNik.Text & "'", "1") Dim FileName As String = TxtFileName.Text 'It 's a file name displayed on downloaded file on client side. Dim unionname As String = "D:/Herlambang/September-Ongoing - Revisi Input Data Izin (Table)/LiveMUGENASP/MugenASPHRISO_10_22_018/img/imgstaff/" + FileName Response.ContentType = "image/jpeg" 'Response.ContentType = "application/octet-stream" Response.AppendHeader("Content-Disposition", "attachment; filename=" & FileName) Response.TransmitFile(unionname) Response.[End]()
it work very well when we upload and download. but when the download finish, the file result of the download always corrupted. i tried to check manualy on server, the file is not corrupted. can you guys show me some suggestion to fix this ?
Monday, October 29, 2018 3:22 AM
Answers
-
User1228272764 posted
Here is the answer for who ever need it :
Call GetData_DetaiIzinStaff(" Select * FROM DATA_IZIN_BODY WHERE IZIN_ID = '" & TxtDetailNik.Text & "'", "1") Dim FileName As String = TxtFileName.Text 'It 's a file name displayed on downloaded file on client side. Dim unionname As String = "C:/inetpub/wwwroot/MugenASPHRISO/WEBDOWNLOAD/FOTOIZIN/" + FileName Response.Write("<script>alert('\Url : " + unionname + "')</script>") Context.Response.Clear() Response.Buffer = True Response.ContentType = "application/jpg" Response.AppendHeader("Content-Disposition", "attachment; filename=" & FileName)
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 27, 2018 7:36 AM
All replies
-
User753101303 posted
Hi,
Try to add Response.Clear as well. My personal preference is to avoid doing downloads from ASPX page. I'm using doing that from an ASPX page. I prefer to use an ASHX handler (see for example https://stackoverflow.com/questions/10912164/what-is-the-best-way-to-download-file-from-server) which allows to skip the while page lifecyle. You can even take advantage of MVC and/or routing so that it looks like you are downloading an actual file.
BTW not directly related but I avoid to use the client side name to store files. If a user (or users) is using the same name multiple times it seems you'll erase existing files...
Monday, October 29, 2018 10:27 AM -
User1228272764 posted
Here is the answer for who ever need it :
Call GetData_DetaiIzinStaff(" Select * FROM DATA_IZIN_BODY WHERE IZIN_ID = '" & TxtDetailNik.Text & "'", "1") Dim FileName As String = TxtFileName.Text 'It 's a file name displayed on downloaded file on client side. Dim unionname As String = "C:/inetpub/wwwroot/MugenASPHRISO/WEBDOWNLOAD/FOTOIZIN/" + FileName Response.Write("<script>alert('\Url : " + unionname + "')</script>") Context.Response.Clear() Response.Buffer = True Response.ContentType = "application/jpg" Response.AppendHeader("Content-Disposition", "attachment; filename=" & FileName)
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 27, 2018 7:36 AM