How to Download A file stored in SQL DB in Binary Format

后端 未结 3 1856
春和景丽
春和景丽 2020-12-19 09:31

I am simply storing uploaded file into a binary field in SQL Server but I also need to allow users to download it with Asp.NET. How can I do that ?

Thanks in advance

3条回答
  •  执笔经年
    2020-12-19 10:09

    Here's a Microsoft Knowledge Base article on this.

    How to retrieve the file from your database depends on the data access technology you use; I will just assume that you have some Byte array data containing the file (e.g. by filling a DataSet and accessing the field) and some string filename.

    Response.Clear()
    Response.ContentType = "application/octet-stream"
    Response.AddHeader("Content-Disposition", "attachment;filename=""" & filename & """")
    Response.BinaryWrite(data)
    Response.End()
    

    Put the above code in some download.aspx and link to this file. You probably want to pass some query string information to your download.aspx, so that your code knows which file to get from the database.

提交回复
热议问题