When downloading a file from ASP .Net, the text file gets appended with HTML content

前端 未结 3 1302
别那么骄傲
别那么骄傲 2021-01-06 02:07

I have made a page that allows users to upload files to the server using a FileUpload Control and handling its event with this code

Sub SaveAttachment()
             


        
3条回答
  •  不思量自难忘°
    2021-01-06 02:55

    Response.redirect threw an exception. Shoaib's answer got me closer but it suppressed everything including the valid file content. What worked for me was reordering the statements as follows:

     Response.ContentType = ContentType
     Response.AppendHeader("Content-Disposition", "attachment; filename=" & originalFileName)
     Response.WriteFile(filePath)
     Response.Flush()
     Response.SuppressContent = True
     HttpContext.Current.ApplicationInstance.CompleteRequest()
    

提交回复
热议问题