Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从WCF服务获取Excel后,流式处理Excel文件以供下载_C#_Asp.net_.net_Excel_Wcf - Fatal编程技术网

C# 从WCF服务获取Excel后,流式处理Excel文件以供下载

C# 从WCF服务获取Excel后,流式处理Excel文件以供下载,c#,asp.net,.net,excel,wcf,C#,Asp.net,.net,Excel,Wcf,在这里遇到了一个问题,我再一次认为我已经接近解决方案了,但它却一直在逃避我 在从WCF服务获取文件流后,我正在尝试从我们的ASP.NET网站下载Excel文件。这似乎很有效,但我可能完全走错了方向 使用代码中的断点,我可以看到文件到达方法并“流”到浏览器,但无法下载(当小图标出现在角落,或出现提示时,具体取决于浏览器) 我已尝试修改此代码以接受.xls文件 这是我的FileTransfer.cs和IFileTransfer.cs WCF类。 FileTransfer.cs public Remo

在这里遇到了一个问题,我再一次认为我已经接近解决方案了,但它却一直在逃避我

在从WCF服务获取文件流后,我正在尝试从我们的ASP.NET网站下载Excel文件。这似乎很有效,但我可能完全走错了方向

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

我已尝试修改此代码以接受.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;
            }
[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;
        }
    }
}
IFileTransfer.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;
            }
[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类型:

此外,在IIS中,如果使用SSL,则在“HTTP标头”选项卡上的“自定义HTTP标头”下,您应该看到指定的“缓存控制无缓存”。您应该删除该选项

您也可以尝试以下方法:

HttpContext.Current.Response.AddHeader("Content-Type: application/octet-stream");
此八位字节流内容类型强制IE下载该文件。

无效:(MIME类型都存在,并且在IIS中也有说明。IIS实现目前也没有使用SSL。我之前也尝试将其设置为“八位字节流”,但它不起作用,正如您所说,我尝试在标头中使用它,仍然无效。