本文介绍了无法上传大小为>的文件使用REST API到SharePoint的10 mb-400错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个要求,我们需要使用REST API将文件上传到SharePoint 2013(内部部署).  但是下面的代码段上传的文件是< 10mb.对于文件> 10mb,出现错误" 400 Bad Request".有人可以建议, 可以使用REST API上传的最大照片.还有一种方法(使用REST API)上传最大50 mb的文件.

We have a requirement where we need to upload the files to the SharePoint 2013(onpremise) using REST API.  However below code snippet is uploading files <10mb. For files >10mb getting error "400 Bad Request". Can some one suggest, the maximum fize that can be uploaded using REST api. Also is there a way(using REST API) to upload files up to 50 mb.

代码段:

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace CheckLargeFileUpload
{
    class Program
    {
        static void Main(string[] args)
        {
            string filePath = @"D:\Logs\FileXYZ-20170703-16181.log"; //File to be uploade
            string siteurl = "https://mysite.com/Sites/ABC"; //site on which file needs to be uploaded (don't put  / at end)
            string documentlibrary = "Documents"; //Document library where file needs to be uploaded
            bool filestatus = UploadUsingRest(siteurl, documentlibrary, filePath);            
            if (filestatus)
            {
                Console.WriteLine("File uploaded successfully");
            }
            else
            {
                Console.WriteLine("Error uploading file");
            }
        }

        //Method to upload File

        

        public static bool UploadUsingRest(string siteurl, string libraryName, string filePath)
        {
            bool status = false;
            byte[] binary = ReadAllBytes(filePath);


            string fname = System.IO.Path.GetFileName(filePath);
            string result = string.Empty;
            //Url to upload file
            string resourceUrl = siteurl + "/_api/web/GetFolderByServerRelativeUrl('" + libraryName + "')/Files/add(url='" + fname + "',overwrite=true)";
            HttpWebRequest wreq = HttpWebRequest.Create(resourceUrl) as HttpWebRequest;
            wreq.UseDefaultCredentials = false;
            //credential who has edit access on document library
            NetworkCredential credentials = new System.Net.NetworkCredential("UserName", "Password", "Domain");
            wreq.Credentials = credentials;

            //Get formdigest value from site
            string formDigest = GetFormDigestValue(siteurl, credentials);
            wreq.Headers.Add("X-RequestDigest", formDigest);
            wreq.Method = "POST";
            wreq.Timeout = 1000000; //timeout should be large in order to upload file which are of large size
            wreq.Accept = "application/json; odata=verbose";
            wreq.ContentLength = binary.Length;
            try
            {
                using (System.IO.Stream requestStream = wreq.GetRequestStream())
                {
                    requestStream.Write(binary, 0, binary.Length);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            try
            {
                WebResponse wresp = wreq.GetResponse();
                using (System.IO.StreamReader sr = new System.IO.StreamReader(wresp.GetResponseStream()))
                {
                    result = sr.ReadToEnd();
                    status = true;
                    return status;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return status;
                throw;
            }
            Console.ReadKey();
        }

        //Method which return form digest value
        private static string GetFormDigestValue(string siteurl, NetworkCredential credentials)
        {
            string newFormDigest = "";
            HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create(siteurl + "/_api/contextinfo");
            endpointRequest.Method = "POST";
            endpointRequest.ContentLength = 0;
            endpointRequest.Credentials = credentials;
            endpointRequest.Accept = "application/json;odata=verbose";

            try
            {
                HttpWebResponse endpointResponse = (HttpWebResponse)endpointRequest.GetResponse();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            try
            {

                WebResponse webResp = endpointRequest.GetResponse();
                Stream webStream = webResp.GetResponseStream();
                StreamReader responseReader = new StreamReader(webStream);
                string response = responseReader.ReadToEnd();
                var j = JObject.Parse(response);
                var jObj = (JObject)JsonConvert.DeserializeObject(response);
                foreach (var item in jObj["d"].Children())
                {
                    newFormDigest = item.First()["FormDigestValue"].ToString();
                }
                responseReader.Close();

            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
            }

            return newFormDigest;
        }


        public static byte[] ReadAllBytes(string fileName)
        {
            byte[] buffer = null;
            using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                buffer = new byte[fs.Length];
                fs.Read(buffer, 0, (int)fs.Length);
            }
            return buffer;
        }
    }
}

推荐答案

我建议您在SharePoint 2013中增加最大上传大小.

I suggest you increase the Maximum Upload Size in SharePoint 2013.

以下是一个博客供您参考:

Here is a blog for your reference:

如何在SharePoint 2013中增加最大上传大小

https://blogs.technet.microsoft.com/sammykailini/2013/11/06/how-to-increase-the-maximum-upload-size-in-sharepoint-2013/

最好的问候,

Dennis


这篇关于无法上传大小为&gt;的文件使用REST API到SharePoint的10 mb-400错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 08:57