C#ASP.NET 3.5内容处置文件下载问题 [英] C# ASP.NET 3.5 content-disposition file download problems

查看:139
本文介绍了C#ASP.NET 3.5内容处置文件下载问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些存储文档的SQL数据库。

I have a sql database that stores some documents.

一个用户可以登录到应用程序,并查看他们的文档的列表。

A user can sign into the application, and view a list of their documents.

当点击了文档的gridview的一个LinkBut​​ton下载,我从数据库中的文件,将其写入到文件系统,然后执行此code。

When clicking a linkbutton download in a gridview of their docs, I get the file from the database, write it to the file system, and then execute this code.

    System.IO.FileInfo file = new System.IO.FileInfo(System.Configuration.ConfigurationManager.AppSettings["UploadPath"] + DocumentName);

    Response.Clear();
    Response.ClearContent();
    Response.ClearHeaders();
    Response.Cookies.Clear();
    Response.Cache.SetCacheability(HttpCacheability.Private);
    Response.CacheControl = "private";
    Response.Charset = System.Text.UTF8Encoding.UTF8.WebName;
    Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
    Response.AppendHeader("Content-Length", file.Length.ToString());
    Response.AppendHeader("Pragma","cache");
    Response.AppendHeader("Expires", "60");
    Response.ContentType = GetContentType(file.Extension);
    Response.AppendHeader("Content-Disposition",
    "inline; " +
    "filename=\"" + file.Name + "\"; " +
    "size=" + file.Length.ToString() + "; " +
    "creation-date=" + DateTime.Now.ToString("R") + "; " +
    "modification-date=" + DateTime.Now.ToString("R") + "; " +
    "read-date=" + DateTime.Now.ToString("R"));

我的getContentType()方法只是返回我允许的应用程序/ PDF格式,应用/ msw0rd,这些文件的相应的文件类型等。

My GetContentType() method just returns the appropriate file type for the files I'm allowing "application/pdf, application/msw0rd, etc.

我的问题是,当该文件被保存,它的网页本身,而不是从文件系统中的文件。而在谷歌浏览器,它是把一个扩展名为.htm的文件名的末尾,我猜是因为它知道这是一个网页?

My problem is that when the file gets saved, it's the webpage itself, not the file from the file system. And in google chrome, it's putting a .htm extension on the end of the filename, I guess because it knows it's a web page?

总之,一个伟大的第一步是获得实际的文件,而不是网页的HTML中他们坐在一个副本!

Anyhow, a great first step would be to get the actual file, and not a copy of the web page in HTML they are sitting on!

感谢。

推荐答案

你是如何发送文件的实际内容?

How are you sending the actual content of the file??

我通常使用 Response.TransmitFile 方法,它基本上打开文件,并发送其内容到Response.OutputStream

I usually use Response.TransmitFile method, it basically opens the file and sends its content to the Response.OutputStream

这篇关于C#ASP.NET 3.5内容处置文件下载问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆