C# 在ASP.net C上下载文件#
我的ASP.net项目中的下载有严重问题。 我在两页中使用相同的代码下载了一个文件:C# 在ASP.net C上下载文件#,c#,javascript,asp.net,ajax,C#,Javascript,Asp.net,Ajax,我的ASP.net项目中的下载有严重问题。 我在两页中使用相同的代码下载了一个文件: private bool DownloadAnhang_Tracking(string fileName, string filepath) { ////File Path and File Name string filePath = filepath; string downloadableFileName = fileName;
private bool DownloadAnhang_Tracking(string fileName, string filepath)
{
////File Path and File Name
string filePath = filepath;
string downloadableFileName = fileName;
System.IO.FileInfo filename;
FileStream myFile;
try
{
filename = new System.IO.FileInfo(filePath);
myFile = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}
catch (IOException ex)
{
this.ShowErrorNotification("Fehler: " + ex.Message);
throw;
}
////Reads file as binary values
BinaryReader binaryReader = new BinaryReader(myFile);
////Check whether file exists in specified location
if (filename.Exists)
{
try
{
long startBytes = 0;
string lastUpdateTiemStamp = File.GetLastWriteTimeUtc(filePath).ToString("r");
string encodedData = HttpUtility.UrlEncode(downloadableFileName, Encoding.UTF8) + lastUpdateTiemStamp;
Response.Clear();
Response.Buffer = false;
Response.AddHeader("Accept-Ranges", "bytes");
Response.AppendHeader("ETag", "\"" + encodedData + "\"");
Response.AppendHeader("Last-Modified", lastUpdateTiemStamp);
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + filename.Name);
Response.AddHeader("Content-Length", (filename.Length - startBytes).ToString());
Response.AddHeader("Connection", "Keep-Alive");
Response.ContentEncoding = Encoding.UTF8;
////Send data
binaryReader.BaseStream.Seek(startBytes, SeekOrigin.Begin);
////Dividing the data in 1024 bytes package
int maxCount = (int)Math.Ceiling((filename.Length - startBytes + 0.0) / 1024);
////Download in block of 1024 bytes
int i;
for (i = 0; i < maxCount && Response.IsClientConnected; i++)
{
Response.BinaryWrite(binaryReader.ReadBytes(1024));
Response.Flush();
}
////if blocks transfered not equals total number of blocks
if (i < maxCount)
{
return false;
}
else
{
return true;
}
}
catch
{
return false;
}
finally
{
Response.End();
binaryReader.Close();
myFile.Close();
}
}
else
{
this.ShowErrorNotification("Der Anhang wurde nicht gefunden!");
}
return false;
}
private bool downloadanhu\u跟踪(字符串文件名、字符串文件路径)
{
////文件路径和文件名
字符串filePath=filePath;
字符串downloadableFileName=文件名;
System.IO.FileInfo文件名;
文件流myFile;
尝试
{
filename=newsystem.IO.FileInfo(filePath);
myFile=newfilestream(filePath,FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
}
捕获(IOEX异常)
{
此通知(“费勒:+ex.Message”);
投掷;
}
////将文件读取为二进制值
BinaryReader BinaryReader=新的BinaryReader(myFile);
////检查文件是否存在于指定位置
if(filename.Exists)
{
尝试
{
长起始字节=0;
字符串LastUpdateTimeStamp=File.GetLastWriteTimeUtc(filePath.ToString(“r”);
字符串encodedData=HttpUtility.UrlEncode(可下载文件名,Encoding.UTF8)+lastUpdateTimesTamp;
Response.Clear();
Response.Buffer=false;
AddHeader(“接受范围”、“字节”);
AppendHeader(“ETag”,“\”“+encodedData+”\);
AppendHeader(“上次修改”,lastUpdateItemStamp);
Response.ContentType=“应用程序/八位字节流”;
Response.AddHeader(“内容处置”、“附件;文件名=“+filename.Name”);
AddHeader(“Content-Length”,(filename.Length-startBytes.ToString());
AddHeader(“连接”,“保持活动”);
Response.ContentEncoding=Encoding.UTF8;
////发送数据
BaseStream.Seek(startBytes,SeekOrigin.Begin);
////将数据划分为1024字节的包
intmaxcount=(int)Math.天花((filename.Length-startBytes+0.0)/1024);
////以1024字节块下载
int i;
对于(i=0;i
所以在另一个页面中使用了这段代码,但它不再工作了!我将相同的信息传递给该方法,但它不起作用!错误来自MSAjax:
0x800a139e-JavaScript中的运行时错误:
Sys.WebForms.PageRequestManagerParserErrorException:消息
无法分析从服务器接收的数据。常见原因
调用response.Write()修改响应时出错,
已启用响应筛选器、HttpModules或服务器跟踪
我不明白为什么这段代码在第1页有效,但在第2页无效。
我使用的是ASP.NET 4.5 Web窗体,带有C<不太确定它会不会是一个问题,但是否是UpDePaNeL中的下载控件?我会照看它,但是我从我的Team Foundation Server恢复时没有遇到问题,我会尽快查看它。阿美:谢谢!没问题,很乐意帮忙:)