简体   繁体   中英

'system.outofmemoryexception' exception on file upload in c#

i am trying to upload a file with 400Mb in sharepoint library from web service using CSOM and getting error 'system.outofmemoryexception' . code is working fine with small size file.

my code is like below

    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;
    }

thanks in advance..

You could try using a FileStream, which only holds a small buffer of bytes in memory instead of the entire file as you are doing now.

How can I read/stream a file without loading the entire file into memory?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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