繁体   English   中英

使用C#上传文件时出现'system.outofmemoryexception'异常

[英]'system.outofmemoryexception' exception on file upload in c#

我正在尝试使用CSOM从Web服务上载sharepoint库中的400Mb文件,并出现错误“ system.outofmemoryexception”。 代码适用于小尺寸文件。

我的代码如下

    try
    {

        HttpContext postedContext = HttpContext.Current;
        HttpFileCollection Files = postedContext.Request.Files;
        string listTitle = postedContext.Request.Form["listTitle"];
        string Industry = postedContext.Request.Form["Industry"];

        if (Files.Count == 1 && Files[0].ContentLength > 0)
        {
            string fileName = Files[0].FileName;
            string docTitle = Files[0].FileName;
            byte[] binaryWriteArray = new byte[Files[0].InputStream.Length];
            string timestamp = string.Format("{0:yyyy-MM-dd_hh-mm-ss-tt}", DateTime.Now);
            fileName = timestamp + "_" + fileName;

            Files[0].InputStream.Read(binaryWriteArray, 0,
            (int)Files[0].InputStream.Length);

            ClientContext context = getContext();

            List documentsList = context.Web.Lists.GetByTitle(listTitle);
            context.Load(documentsList.RootFolder);
            context.ExecuteQuery();

            var fileCreationInformation = new FileCreationInformation();

            fileCreationInformation.Content = binaryWriteArray;


            fileCreationInformation.Overwrite = true;
            //Upload URL

            fileCreationInformation.Url = String.Format("{0}/{1}", documentsList.RootFolder.ServerRelativeUrl, fileName);
            Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(
                fileCreationInformation);


            uploadFile.ListItemAllFields["ToolId"] = Convert.ToString(postedContext.Request.Form["toolId"]);
            uploadFile.ListItemAllFields["Industry"] = Convert.ToString(postedContext.Request.Form["Industry"]);
            uploadFile.ListItemAllFields["Title"] = docTitle;

            uploadFile.ListItemAllFields.Update();
            context.ExecuteQuery();


            return true;
        }
        else
        {
            return false;
        }



    }
    catch (Exception ex)
    {
        General.BindErrorLog(ex);
        return false;
    }

提前致谢..

您可以尝试使用FileStream,该文件流仅在内存中保留一小部分字节缓冲区,而不是像现在一样在整个文件中。

如何在不将整个文件加载到内存的情况下读取/流式传输文件?

暂无
暂无

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

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