简体   繁体   English

使用WCF服务将大文件传输到Sharepoint WebPart

[英]Transfer big files with WCF service to sharepoint webpart

Scenario: The client (SharePoint Webpart) should provide a download to the user. 方案:客户端(SharePoint Webpart)应该向用户提供下载。 Because of security reasons I can't access the file directly from the SharePoint Webpart. 由于安全原因,我无法直接从SharePoint Webpart访问文件。 So I created an wcf webservice which sends the file as stream. 因此,我创建了一个wcf Web服务,该服务将文件作为流发送。 But this dosen't work with bigger files. 但这不适用于较大的文件。

WCF Webservice send Methode: WCF Web服务发送方法:

public Stream GetFileStream(string filePath)
{
        WebClient client = new WebClient();
        return client.OpenRead(filePath);
}

The client (SharePoint Webpart) get the stream and convert it in byte[] so I can create a download in the browser. 客户端(SharePoint Webpart)获取流并将其转换为byte [],因此我可以在浏览器中创建下载。

private void SendFileToBrowser(string filePath)
    {
        Page.Response.Clear();
        Page.Response.ClearContent();
        Page.Response.ClearHeaders();
        Page.Response.ContentType = "application/pdf";
        Page.Response.AddHeader("Content-Disposition", string.Format("attachment; fileName=\"{0}\"", Path.GetFileName(filePath)));
        Page.Response.AddHeader("Accept-Ranges", "bytes");

        byte[] buffer;

        CustomBinding binding = new CustomBinding();
        BinaryMessageEncodingBindingElement binaryMsg = new BinaryMessageEncodingBindingElement();
        binaryMsg.ReaderQuotas.MaxDepth = 2147483647;
        binaryMsg.ReaderQuotas.MaxStringContentLength = 2147483647;
        binaryMsg.ReaderQuotas.MaxArrayLength = 2147483647;
        binaryMsg.ReaderQuotas.MaxBytesPerRead = 2147483647;
        binaryMsg.ReaderQuotas.MaxNameTableCharCount = 2147483647;
        binding.Elements.Add(binaryMsg);

        HttpTransportBindingElement transportBinding = new HttpTransportBindingElement();
        transportBinding.TransferMode = TransferMode.Streamed;
        transportBinding.MaxReceivedMessageSize = 2147483647;
        binding.Elements.Add(transportBinding);

        EndpointAddress endPoint = new EndpointAddress ("http://xxx/fileservice.svc");
        FileServiceClient fileServiceProxy = new FileServiceClient(binding, endPoint);

        // Stream
        Stream stream = fileServiceProxy.GetFileStream(filePath);

        fileServiceProxy.Close();

        // convert stream to byte[]
        using (var memoryStream = new MemoryStream())
        {
            stream.CopyTo(memoryStream);
            buffer = memoryStream.ToArray();
        }

        if (buffer != null)
        {
            Page.Response.BinaryWrite(buffer);
        }

        Page.Response.Flush();
        Page.Response.Close();
        Page.Response.End();
    }

The code works if the files are smaller > 300kb. 如果文件小于300kb,则此代码有效。 But if the files are bigger (eg 50mb) it dosen't work always. 但是,如果文件较大(例如50mb),则可能无法始终正常工作。 But it works sometimes!??? 但这有时有效! I don't not what's wrong? 我不是怎么了?

After some testing I uncommented the following 2 lines at the end. 经过一些测试后,我最后没有评论以下两行。 Now it is working. 现在正在工作。

    Page.Response.Flush();
    //Page.Response.Close();
    //Page.Response.End();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM