Getting error while downloading the file using Response.TransmitFile in ASP.NET

Gani_tpt 1,506 Reputation points
2022-06-29T10:49:23.51+00:00

I am trying to download .asd file freom the respective location.

If i opened direct file from the folder, i can able to open.

But, when i open the file from the application, after downloading the file, getting below error (after downloading and open the file) Error ==> "Sc1.asd could not be opened because the contents are corrupt or it is currently unavailable."

screenshot

216154-image.png

source code

        filename = "\\cas1.cds.intranet\zone1\reg10\projA\Sc1.asd"  
        Response.Clear()  
        Using oBinaryReader As BinaryReader = New BinaryReader(File.OpenRead(filename))  
            Response.ContentType = "application/octet-stream"  
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + System.IO.Path.GetFileName(filename))  
            Response.TransmitFile(filename)  
            Response.Flush()  
        End Using  
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,243 questions
{count} votes

Accepted answer
  1. Albert Kallal 4,646 Reputation points
    2022-06-30T17:42:57.993+00:00

    Ok, a quick google of transmits file and corruption?

    Well, turns out - common problem.

    First up, PDF and a lot of file formats have some "wiggle" room in terms of the file size in bytes sent. However, some binary commercial programs? They don't tolerate a SLIGHT change in the file size saved, or sent. In fact, in the old days, FTP would often cause this issue (the exact file size and length was padded as a multiple of 256. As noted, most software does not care but some does!!!

    Also, be careful. Reponse.Flush, and Response.End have HUGE different meanings here.
    Don't just throw mud against the wall - hope this works. Figure out WHICH one is more appropriate for your needs.

    So, the solution suggested for this issue? INCLUDE the file size in the headers for the download.

    Try this code :

           If File.Exists(strInternalFile) Then  
      
                Dim iFileInfo As New FileInfo(strInternalFile)  
      
                strConType = MimeMapping.GetMimeMapping(strInternalFile)  
      
                Response.Clear()  
                Response.ContentType = strConType  
                Response.AppendHeader("Content-Length", iFileInfo.Length.ToString)  
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(strInternalFile))  
                Response.TransmitFile(strInternalFile)  
                Response.End()  
      
            Else  
                ' send jquery toast message - file not found.  
                ' grab control  few colums left of the "right most image button"  
                ' We just need a good toast popup position (if I use image button - toast message is too far right side  
                ' so grab the Qty column - anything a few columns to the left is fine  
      
    

    it should be Mr. Obvious in above, but that Response.End is the LAST LINE AND ONLY line of code to be executed here - anything else after (more code) should NOT exist, nor be required BUT ALSO NOT RUN!!!!!

    In other words, because we are dealing with the page life cycle, then MUCH concern as to what occurs in the page load event needs caution. (any code that loads up the page, the grid, the tree view or ANYTHING else needs to be placed inside of the page load even wrapped in the ALL super duper important Is not page post back).

    Eg this:

       Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
      
      
            If IsPostBack = False Then  
      
                ' code here to load gride, or tree view etc. - ONLY FIRST page load!!  
      
            End if  
    

0 additional answers

Sort by: Most helpful