【问题标题】:Stream Excel File for Download after getting Excel from WCF Service从 WCF 服务获取 Excel 后流式传输 Excel 文件以供下载
【发布时间】:2013-12-13 22:11:56
【问题描述】:

这里有问题,再次认为我接近解决方案,但它正在躲避我。

从 WCF 服务获取文件流后,我尝试从我们的 ASP.NET 网站下载 Excel 文件。这似乎工作正常,但我可能会走错路。

使用代码中的断点,我可以看到文件到达方法并“流式传输”到浏览器,但是它无法下载(当小图标出现在角落或会出现提示,具体取决于浏览器)。

我已尝试修改此代码 http://www.codeproject.com/Articles/166763/WCF-Streaming-Upload-Download-Files-Over-HTTP 以接受 .xls 文件。

这是我的 FileTransfer.cs 和 IFileTransfer.cs WCF 类。 文件传输.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;
        }
    }
}

这里是链接按钮方法/事件。

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();
    }
}

【问题讨论】:

    标签: c# asp.net .net excel wcf


    【解决方案1】:

    我假设您已经为您的 excel 文件配置了 MIME 类型。您可以从这里查看如何创建 MIME 类型:

    http://technet.microsoft.com/en-us/library/cc725608(WS.10).aspx

    此外,如果您使用 SSL,则在您的 IIS 中,在 HTTP Headers 选项卡上的 Custom HTTP Headers 下,您应该看到指定的“Cache-Control no-cache”。您应该删除该选项。

    你也可以试试这个:

    HttpContext.Current.Response.AddHeader("Content-Type: application/octet-stream");
    

    这种八位字节流内容类型强制 IE 下载文件。

    【讨论】:

    • 没有任何效果 :( MIME 类型都存在并且在 IIS 中也有说明。IIS 实现目前也没有使用 SSL。我之前也尝试将其设置为“octet-stream”并且它没有用,我尝试在标题中使用它,如你所说,仍然没有。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    相关资源
    最近更新 更多