标签: response.transmitfile

Response.TransmitFile并在传输后删除它

我必须在我的网站上实施GEDCOM导出.

单击导出到gedcom时,我的.net代码在服务器上创建了一个文件.

然后我需要从服务器下载它到客户端,并且应该询问用户保存该文件的位置,这意味着需要savedialog.

下载后,我想从服务器删除该文件.

我有一个代码将文件从服务器传输到客户端:

Response.ContentType = "text/xml";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
Response.TransmitFile(Server.MapPath("~/" + FileName));
Response.End();
Run Code Online (Sandbox Code Playgroud)

从这个链接

但是我无法在此代码之后删除文件作为Response.End结束响应,因此在该行之后写入的任何代码都不会执行.

如果我之前执行代码删除文件Response.End();,则文件不会传输,我收到错误.

c# asp.net file-io gedcom response.transmitfile

8
推荐指数
1
解决办法
2万
查看次数

Repsonse.Transmitfile(); 能够节省但无法打开

我正在尝试使用发送xlsx文件

Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/vnd-ms.excel";
Response.TransmitFile(file.FullName);
Response.End();
Run Code Online (Sandbox Code Playgroud)

弹出IE对话框,我可以成功保存文件,然后从文件夹打开它,工作正常和花花公子.但如果我在IE对话框中单击"打开",我会收到"无法下载myFile.xlsx".我单击"重试"并打开Excel但弹出"Excel无法打开文件'myFile.xlsx',因为文件格式或文件扩展名无效..."错误.
我目前正在调试模式下从VS2010运行该站点.

有谁知道为什么它会让我保存,但不能直接打开?

编辑
Chrome只需下载它.FF尝试打开它但是给出错误The file you are trying to open, 'myFile.xlsx.xls', is in a different format than specified by the file extension...我可以选择打开它并且它成功打开,但是在只读模式下.
所以,这里有一些时髦的东西.
fileName ="myFile.xlsx"

编辑2
这是在IE 9中.我也尝试过octet-streamapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet作为ContentType.

c# asp.net response.transmitfile

8
推荐指数
1
解决办法
5978
查看次数

高效地从asp.net core发送文件

我有兴趣将一些代码移植到 ASP.NET Core,并想知道从 ASP.NET Core Web 服务发送文件(也称为“下载”文件)的最有效方法。

使用旧的 ASP.NET 代码,我使用的是 FileStream:

var content = new FileStream(
    myLocation,
    FileMode.Open, FileAccess.Read, FileShare.Read);

var result = new HttpResponseMessage(HttpStatusCode.OK)
{
    Content = new StreamContent(content)
};
Run Code Online (Sandbox Code Playgroud)

但是,我试图找到与 FreeBSD 的 sendfile() 等效的 .NET 并找到HttpResponse.TransmitFile。我认为这会更快?

我还担心该文件在访问用户之前必须从 Kestrel 跳到 IIS。有什么建议吗?

c# asp.net response.transmitfile asp.net-web-api asp.net-core

8
推荐指数
1
解决办法
7986
查看次数

Response.TransmitFile不下载,并且没有抛出错误

我目前正在使用HttpResponse从我的服务器下载文件.我已经有几个函数用于下载Excel/Word文件,但我无法下载简单的文本文件(.txt).

使用文本文件,我基本上将TextBox的内容转储到文件中,尝试使用HttpResponse下载文件,然后删除临时文本文件.

以下是适用于Excel/Word文档的代码示例:

protected void linkInstructions_Click(object sender, EventArgs e)
{
    String FileName = "BulkAdd_Instructions.doc";
    String FilePath = Server.MapPath("~/TempFiles/BulkAdd_Instructions.doc");
    System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
    response.ClearContent();
    response.Clear();
    response.ContentType = "application/x-unknown";
    response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
    response.TransmitFile(FilePath);
    response.Flush();
    response.End();  
}
Run Code Online (Sandbox Code Playgroud)

这里有一大堆代码不起作用.
请注意,代码运行时不会出现任何错误.文件已创建并已删除,但从未转储给用户.

protected void saveLog(object sender, EventArgs e)
{ 
    string date = DateTime.Now.ToString("MM_dd_yyyy_hhmm");     //  Get Date/Time
    string fileName = "BulkLog_"+ date + ".txt";                //  Stitch File Name + Date/Time
    string logText = errorLog.Text;                             //  Get Text from TextBox
    string halfPath = "~/TempFiles/" …
Run Code Online (Sandbox Code Playgroud)

c# httpresponse text-files transmitfile response.transmitfile

7
推荐指数
3
解决办法
3万
查看次数

当我使用ASP.NET传输文件时,为什么IIS7会忽略我的内容类型标头?

我有一个有两页的简单网站.一个显示文件列表,另一个文件在列表中单击时流式传输.生产6个月都很好,但现在我必须将网站移动到Windows 2008/IIS7.我有它主要工作,但文件无法正常打开(在Firefox中)因为我的内容类型标题被忽略.在生产站点(IIS6)上,标题是(使用Fiddler):

HTTP/1.1 200 OK
Date: Tue, 09 Feb 2010 16:00:51 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Content-Disposition: attachment; filename="myfile__foo.pdf"
Content-Length: 236841
Cache-Control: no-cache, no-store
Pragma: no-cache
Expires: -1
Content-Type: application/octet-stream
Run Code Online (Sandbox Code Playgroud)

但在测试IIS7服务器上我得到:

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 236841
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 2.0.50727
Content-Disposition: attachment; filename="myfile__foo.pdf"
Run Code Online (Sandbox Code Playgroud)

Fiddler还报告违反协议并说"内容长度不匹配:响应标头声称236841字节,但服务器发送了238378字节."

我的代码看起来像这样:

            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=\"" + doc.DisplayFilename + "." + doc.FileExtension + "\"");
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.TransmitFile(file.FullName);
            Response.End();
Run Code Online (Sandbox Code Playgroud)

我一直试图解决这个问题几天.我对IIS7并不熟悉,并且很难找到改变各种设置的位置.我确实设法弄清了两种管理流水线模式,并通过切换到Classic .NET AppPool修复了一些其他问题(Integrated在传输文件时抛出了各种错误).

有没有办法告诉IIS7不要覆盖我的ContentType标头,如果这是发生了什么?

asp.net iis-7 content-type download response.transmitfile

5
推荐指数
1
解决办法
7632
查看次数

如何从c#代码传输zip文件

我正在使用c#.net应用程序,我需要使用c#codebase下载一个zip文件.我使用以下代码下载文件:

Response.ContentType = "application/zip"                       
Response.AppendHeader("Content-Disposition", string.Format("attachment; filename = {0}", System.IO.Path.GetFileName(sZipFileName)));
Response.TransmitFile(sZipFilePath);
HttpContext.Current.ApplicationInstance.CompleteRequest();
Run Code Online (Sandbox Code Playgroud)

zip文件被传输但是当我在下载后尝试打开zip文件时,我收到一条错误消息"无法打开文件:文件看起来不是有效的存档"

请让我知道我在哪里做错了如何获取一个zip文件并提取它没有任何错误.

提前致谢

c# asp.net zip download response.transmitfile

2
推荐指数
1
解决办法
1万
查看次数

包含空格的文件名的TransmitFile

在C#ASP.Net网站中,要将文件传输到我正在使用的客户端

    Stirng file_path=Server.MapPath("~/files/"+file_name); 
    HttpContext.Current.Response.ContentType = "application/octet-stream";
    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + file_Name);
    HttpContext.Current.Response.TransmitFile(file_path);        
    HttpContext.Current.Response.End();
Run Code Online (Sandbox Code Playgroud)

它工作正常,但是当文件名包含任何空格时,下载的文件只有第一个单词的名称.例如:如果我的文件名是"This is demo.txt",则下载的文件名将变为"This"而没有扩展名.因此,下载它的用户无法识别其类型.
我们怎样才能避免因包含空格的文件名而发生?

我试过用

Stirng file_path="'"+Server.MapPath("~/files/"+file_name)+"'"; 
Run Code Online (Sandbox Code Playgroud)

但没有工作.

编辑:
我也不能替换(用'_'或' - ')或删除服务器上存在的文件名中的所有空格.非常感谢一点帮助.

asp.net string httpresponse response.transmitfile

2
推荐指数
1
解决办法
2724
查看次数