从WCF服务越来越Excel中后流Excel文件的下载 [英] Stream Excel File for Download after getting Excel from WCF Service

查看:118
本文介绍了从WCF服务越来越Excel中后流Excel文件的下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里有一个问题,再次想到我靠近一个解决方案,但它是我躲避

我试图下载从我们的网站上ASP.NET Excel文件从WCF服务得到FILESTREAM之后。这似乎工作不错,但我可能去关于它的完全错误的方式。

在code,我能够看到该文件的方法到达和正在使用断点流到浏览器,但它不成为可供下载(当小图标出现在角落,或将出现一个提示,这取决于浏览器)。

我试着去适应这个code的http://www.$c$cproject.com/Articles/166763/WCF-Streaming-Upload-Download-Files-Over-HTTP接受.xls文件。

下面是我的FileTransfer.cs和IFileTransfer.cs WCF类。
FileTransfer.cs

 公共RemoteFileData DownloadFile(DownloadRequest要求)
            {
                RemoteFileData结果=新RemoteFileData();
                尝试
                {
                    字符串文件路径= System.IO.Path.Combine(@C:\\文件,request.FileName);
                    System.IO.FileInfo的fileInfo =新System.IO.FileInfo(文件路径);                    //不存在的文件?
                    如果(!fileInfo.Exists)
                        抛出新System.IO.FileNotFoundException(找不到文件,request.FileName);                    //打开流
                    System.IO.FileStream流=新System.IO.FileStream(文件路径,System.IO.FileMode.Open,System.IO.FileAccess.Read);                    //返回结果比流
                    result.FileName = request.FileName;
                    result.Length = fileInfo.Length;
                    result.FileByteStream =流;
                }
                赶上(异常前)
                {
                }
                返回结果;
            }

IFileTransfer.cs

  [的ServiceContract]公共接口IFileTransfer
{
    [OperationContract的]
    RemoteFileData DownloadFile(DownloadRequest要求);
}[MessageContract]
公共类DownloadRequest
{
    [MessageBodyMember]
    公共字符串文件名;
}[MessageContract]
公共类RemoteFileData:IDisposable接口
{
    [的MessageHeader(MustUnderstand的= TRUE)]
    公共字符串文件名;    [的MessageHeader(MustUnderstand的= TRUE)]
    众长长度;    [MessageBodyMember(订单= 1)]
    公众的System.IO.Stream FileByteStream;    公共无效的Dispose()
    {
        如果(FileByteStream!= NULL)
        {
            FileByteStream.Close();
            FileByteStream = NULL;
        }
    }
}

这里是LinkBut​​ton的方法/事件。

 保护无效LnkBtn_genPAFFile_Click(对象发件人,EventArgs的发送)
{
    尝试
    {
        GSIISFileTransferService.IFileTransfer tClient =新GSIISFileTransferService.FileTransferClient();
        GSIISFileTransferService.DownloadRequest =的RequestData新GSIISFileTransferService.DownloadRequest();
        GSIISFileTransferService.RemoteFileData的fileInfo =新GSIISFileTransferService.RemoteFileData();        requestData.FileName =form.xls;
        的fileInfo = tClient.DownloadFile(的RequestData);        Response.BufferOutput = FALSE;
        字节[]缓冲区=新的字节[6500];
        INT读取动作= 0;        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.ContentType =应用程序/ vnd.ms-EXCEL;
        HttpContext.Current.Response.AddHeader(内容处置,附件;文件名=+ requestData.FileName);        读取动作= fileInfo.FileByteStream.Read(缓冲液,0,buffer.Length);        而(读取动作大于0)
        {
            如果(Response.IsClientConnected)
            {
                Response.OutputStream.Write(缓冲液,0,读取动作);
                //刷新数据的HTML
                Response.Flush();                缓冲区=新的字节[6500];
                读取动作= fileInfo.FileByteStream.Read(缓冲液,0,buffer.Length);
            }
            其他
            {
                读取动作= -1;
            }
        }
    }
    赶上(异常前)
    {
        System.Web.HttpContext.Current.Response.Write(错误:+ ex.Message);
    }
    最后
    {
        Response.Flush();
        Response.Close();
        到Response.End();
        System.Web.HttpContext.Current.Response.Close();
    }
}


解决方案

我假设你已经为你的Excel文件中配置的MIME类型。您可以点击此处查看如何创建一个MIME类型:

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

另外在你的IIS,如果你使用的是SSL,HTTP标题选项卡上,在自定义HTTP头,应该可以看到指定的缓存控制无缓存。您应该删除该选项。

你也可以试试这个:

  HttpContext.Current.Response.AddHeader(内容类型:应用程序/八位字节流);

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

Got a problem here, and once again think I am close to a solution, however it is eluding me.

I am trying to download an Excel file from our ASP.NET website after getting the filestream from a WCF service. This seems to work okay, but I may be going the entirely wrong way about it.

Using breakpoints in the code I am able to see that the file is arriving in the method and being "streamed" to the browser, however it is not becoming available for download (when the little icon appears in the corner, or a prompt appears, depending on browser).

I've tried to adapt this code http://www.codeproject.com/Articles/166763/WCF-Streaming-Upload-Download-Files-Over-HTTP to accept .xls files.

Here is my FileTransfer.cs and IFileTransfer.cs WCF classes. 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;
        }
    }
}

And here is the Linkbutton method/event.

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

解决方案

I assume that you have configured the MIME type for your excel file. You can check it from here how to create a MIME type:

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

In addition in your IIS if you are using SSL, on the HTTP Headers tab, under Custom HTTP Headers, you should see "Cache-Control no-cache" specified. You should remove that option.

Also you can try this:

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

This octet-stream content type forces IE to download the file.

这篇关于从WCF服务越来越Excel中后流Excel文件的下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆