从数据库图像字段保存文件
我已将文本文件上传到数据库的图像字段中。问题是当我尝试检索文件时(使用户能够通过单击链接下载它)。当我尝试下载它时,它只是填充了网页的内容(所有 html 都填充到文本文件中)。我认为这是我尝试下载文件的方式错误。是否可以将内容流式传输到文件而不先将其保存到临时文件中?
我正在使用的代码如下所示。 UploadFiles 是我的类,其中包含数据、id、名称等。
public void DownloadUploadedFile(Page sender, UploadFiles uf)
{
sender.Response.ContentType = uf.FileType; // the binary data
sender.Response.AddHeader("Content-Disposition", "attachment; filename=" + uf.FileName);
sender.Response.BinaryWrite(uf.FileData);
}
I have uploaded a text file to my database into a Image field. The problem is when I try to retrieve the file (make the user able to download it on a click on a link). When I try to download it, it is simply filled with the content of the webpage (all the html is filled into the text file). I am considering it to be an error with the way I try to download the file. Isn't it possible to stream the content to a file, without first saving it into a temporary file?
The code I am using is shown below. UploadFiles is my class which contains the data, id, name etc.
public void DownloadUploadedFile(Page sender, UploadFiles uf)
{
sender.Response.ContentType = uf.FileType; // the binary data
sender.Response.AddHeader("Content-Disposition", "attachment; filename=" + uf.FileName);
sender.Response.BinaryWrite(uf.FileData);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您有太多页面的其余部分发送给客户端。您需要清除响应,然后关闭它;尝试:
或者,使用处理程序(ashx)来执行此操作 - 因为它不包括常规页面标记。
It sounds like you've got too much of the rest of the page going to the client. You need to clear the response, and close it afterwards; try:
Alternatively, use a handler (ashx) to do this - since that doesn't include the regular page markup.