簡體   English   中英

從WCF服務獲取Excel后流式傳輸Excel文件以進行下載

[英]Stream Excel File for Download after getting Excel from WCF Service

在這里遇到了一個問題,再次認為我已經接近解決方案,但是這使我難以理解。

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

我假設您已經為excel文件配置了MIME類型。 您可以從此處檢查如何創建MIME類型:

http://technet.microsoft.com/zh-CN/library/cc725608(WS.10).aspx

另外,如果您使用的是SSL,則在IIS中的“ HTTP頭”選項卡上“自定義HTTP頭”下,您應該看到指定了“ Cache-Control no-cache”。 您應該刪除該選項。

您也可以嘗試以下方法:

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

此八位字節流內容類型強制IE下載文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM