在这里遇到问题,再一次认为我接近解决方案,但是它让我不知所措。
我正在尝试从WCF服务获取文件流后从我们的ASP.NET网站下载Excel文件。这似乎工作正常,但我可能会采取完全错误的方式。
在代码中使用断点我能够看到文件到达方法并被“流式”传输到浏览器,但是它无法下载(当角落里出现小图标时,或者出现提示,具体取决于浏览器。)
我尝试调整此代码http://www.codeproject.com/Articles/166763/WCF-Streaming-Upload-Download-Files-Over-HTTP以接受.xls文件。
这是我的FileTransfer.cs和IFileTransfer.cs WCF类。 FileTransfer.cs
public RemoteFileData DownloadFile(DownloadRequest request)
{
RemoteFileData result = new RemoteFileData();
try
{
string filePath = System.IO.Path.Combine(@"C:\Files", request.FileName);
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
// does file exist?
if (!fileInfo.Exists)
throw new System.IO.FileNotFoundException("File not found", request.FileName);
// open the stream
System.IO.FileStream stream = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
// return result over the stream
result.FileName = request.FileName;
result.Length = fileInfo.Length;
result.FileByteStream = stream;
}
catch (Exception ex)
{
}
return result;
}
IFileTransfer.cs
[ServiceContract]
public interface IFileTransfer
{
[OperationContract]
RemoteFileData DownloadFile(DownloadRequest request);
}
[MessageContract]
public class DownloadRequest
{
[MessageBodyMember]
public string FileName;
}
[MessageContract]
public class RemoteFileData : IDisposable
{
[MessageHeader(MustUnderstand = true)]
public string FileName;
[MessageHeader(MustUnderstand = true)]
public long Length;
[MessageBodyMember(Order = 1)]
public System.IO.Stream FileByteStream;
public void Dispose()
{
if (FileByteStream != null)
{
FileByteStream.Close();
FileByteStream = null;
}
}
}
这是Linkbutton方法/事件。
protected void LnkBtn_genPAFFile_Click(object sender, EventArgs e)
{
try
{
GSIISFileTransferService.IFileTransfer tClient = new GSIISFileTransferService.FileTransferClient();
GSIISFileTransferService.DownloadRequest requestData = new GSIISFileTransferService.DownloadRequest();
GSIISFileTransferService.RemoteFileData fileInfo = new GSIISFileTransferService.RemoteFileData();
requestData.FileName = "form.xls";
fileInfo = tClient.DownloadFile(requestData);
Response.BufferOutput = false;
byte[] buffer = new byte[6500];
int bytesRead = 0;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + requestData.FileName);
bytesRead = fileInfo.FileByteStream.Read(buffer, 0, buffer.Length);
while (bytesRead > 0)
{
if (Response.IsClientConnected)
{
Response.OutputStream.Write(buffer, 0, bytesRead);
// flush data to HTML
Response.Flush();
buffer = new byte[6500];
bytesRead = fileInfo.FileByteStream.Read(buffer, 0, buffer.Length);
}
else
{
bytesRead = -1;
}
}
}
catch (Exception ex)
{
System.Web.HttpContext.Current.Response.Write("Error : " + ex.Message);
}
finally
{
Response.Flush();
Response.Close();
Response.End();
System.Web.HttpContext.Current.Response.Close();
}
}
答案 0 :(得分:0)
我假设您已为excel文件配置了MIME类型。您可以从此处查看如何创建MIME类型:
http://technet.microsoft.com/en-us/library/cc725608(WS.10).aspx
此外,在您的IIS中,如果您使用的是SSL,则在“HTTP标头”选项卡上的“自定义HTTP标头”下,您应该看到" Cache-Control no-cache"指定。你应该删除该选项。
你也可以试试这个:
HttpContext.Current.Response.AddHeader("Content-Type: application/octet-stream");
此八位字节流内容类型强制IE下载文件。