簡體   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