简体   繁体   English

从WCF C#中的URL下载zip文件

[英]downloading a zip file from URL in wcf c#

I am trying to download a zip folder from a url. 我正在尝试从URL下载一个zip文件夹。 The url points to a library in Sharepoint containing a document set. 该URL指向Sharepoint中包含文档集的库。 If the url is pasted in browser it downloads a zip file. 如果将网址粘贴到浏览器中,则会下载一个zip文件。 While trying to do the same from code i am able to download only 32426 bytes. 在尝试通过代码执行相同操作时,我只能下载32426个字节。 I tried two approaches - one the DownloadDataAsync() using WebClient and other the WebRequest and response. 我尝试了两种方法-一种使用WebClient的DownloadDataAsync(),另一种使用WebRequest和响应。 Both these read only 32426 bytes whereas the zip folder is in close to 6 MB. 这两个文件都只能读取32426个字节,而zip文件夹中的文件大小接近6 MB。

using (var Webclient1 = new WebClient())
{
    Webclient1.Headers.Add("Accept: text/html, application/xhtml+xml, */*");
    Webclient1.Headers.Add("User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");

    byte[] data = null;
    Webclient1.DownloadDataCompleted +=
    delegate(object sender, DownloadDataCompletedEventArgs e)
    {
        data = e.Result;
    };

    Webclient1.DownloadDataAsync(uri);
    while (Webclient1.IsBusy)
    {
       System.Threading.Thread.Sleep(10000);
    }

    var len = data.Length;
}

Using HttpRequest and response 使用HttpRequest和响应

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
//request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
request.Accept = @"text/html, application/xhtml+xml, */*";
request.UserAgent = @"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)";

request.Timeout = 1000000;
using (var response = request.GetResponse())
{
    MemoryStream stream2 = new MemoryStream();
    var stream = response.GetResponseStream();
    stream.CopyTo(stream2);
    return stream2.ToArray();
}

Both read incomplete content. 两者都读取不完整的内容。

This should work: 这应该工作:

public bool DownloadFile(string itemUrl, string localPath)
        {
            bool downloadSuccess = false;

            try
            {
                using (IOFile.Stream itemFileStream = GetItemAsStream(itemUrl))
                {
                    using (IOFile.Stream localStream = IOFile.File.Create(localPath))
                    {
                        itemFileStream.CopyTo(localStream);
                        downloadSuccess = true;
                    }
                }
            }
            catch (Exception err)
            {

            }

            return downloadSuccess;
        }


protected IOFile.Stream GetItemAsStream(string itemUrl)
        {
            IOFile.Stream stream = null;
            try
            {
                FileInformation fileInfo = File.OpenBinaryDirect(_context, itemUrl);
                stream = fileInfo.Stream;                
            }
            catch (Exception err)
            {
                throw new ApplicationException(string.Format("Error executing method {0}. {1}", MethodBase.GetCurrentMethod().Name, err.Message));
            }

            return stream;
        }

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

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