我的需求是这样的,开发winform程序,程序中有个功能是下载SharePoint文档库中的文档。所以,毫无疑问,自然要用到SharePoint 的 web service来实现了,即Copy.asmx。
我的代码是这样的:
public static void CopyfileToLocal(string strSiteUrl,string strFileUrl, string strFilePath)
{
using (Copy copyFile = new Copy())
{
if (AdminNwc == null)
{
return;
}
copyFile.Credentials = AdminNwc;
copyFile.Url = strSiteUrl.Trim('/') + @"/_vti_bin/Copy.asmx";
FieldInformation fileInfo = new FieldInformation();
FieldInformation[] fileInfoArray = { fileInfo };
byte[] fileContents;
copyFile.GetItem(strFileUrl, out fileInfoArray, out fileContents);
}
}
(备注:这里关于创建Copy对象都有引用相应的命名空间)
以上代码在执行过程中,小文件都是没有问题的。但遇到了大文件就会抛异常,例如,下载一个180M的文件,里面出错:Exception of type 'System.OutOfMemoryException' was thrown.
请各位园友伸出援助之手。谢谢,不甚感激!
3 个解决方案
#1
用Web service下载大文件肯定会内存溢出,试试:
//
// CopyStream is from
// http://*.com/questions/411592/how-do-i-save-a-stream-to-a-file
//
public static void CopyStream(Stream input, Stream output) {
byte[] buffer = new byte[8 * 1024];
int len;
while ((len = input.Read(buffer, 0, buffer.Length)) > 0) {
output.Write(buffer, 0, len);
}
}
protected void Page_Load(object sender, EventArgs e)
{
string url = "https://myserver.com/test/Shared%20Documents/mypic.jpg";
WebRequest request = WebRequest.Create(new Uri(url, UriKind.Absolute));
request.UseDefaultCredentials = true;
WebResponse response = request.GetResponse();
Stream fs = response.GetResponseStream() as Stream;
using (FileStream localfs = File.OpenWrite(@"c:\temp\aspdownloadedfile.jpg"))
{
CopyStream(fs, localfs);
}
}
#2
因为我的是在winform中做的下载文档,所以不知道这种方法是否可行,先谢谢你,我现在去测试。
#3
太棒了,问题解决了,谢谢你。。。。
我用blog记录了这个问题:
http://www.cnblogs.com/qixing_gan/archive/2013/03/28/2986730.html
#1
用Web service下载大文件肯定会内存溢出,试试:
//
// CopyStream is from
// http://*.com/questions/411592/how-do-i-save-a-stream-to-a-file
//
public static void CopyStream(Stream input, Stream output) {
byte[] buffer = new byte[8 * 1024];
int len;
while ((len = input.Read(buffer, 0, buffer.Length)) > 0) {
output.Write(buffer, 0, len);
}
}
protected void Page_Load(object sender, EventArgs e)
{
string url = "https://myserver.com/test/Shared%20Documents/mypic.jpg";
WebRequest request = WebRequest.Create(new Uri(url, UriKind.Absolute));
request.UseDefaultCredentials = true;
WebResponse response = request.GetResponse();
Stream fs = response.GetResponseStream() as Stream;
using (FileStream localfs = File.OpenWrite(@"c:\temp\aspdownloadedfile.jpg"))
{
CopyStream(fs, localfs);
}
}
#2
因为我的是在winform中做的下载文档,所以不知道这种方法是否可行,先谢谢你,我现在去测试。
#3
太棒了,问题解决了,谢谢你。。。。
我用blog记录了这个问题:
http://www.cnblogs.com/qixing_gan/archive/2013/03/28/2986730.html