Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将文件上载到GLPI服务器POST_MAX_SIZE_C#_Rest_Api_File Upload - Fatal编程技术网

C# 将文件上载到GLPI服务器POST_MAX_SIZE

C# 将文件上载到GLPI服务器POST_MAX_SIZE,c#,rest,api,file-upload,C#,Rest,Api,File Upload,我试图通过API REST将任何文件类型的文档发布到GLPI服务器 以下是我正在做的: private void button11_Click(object sender, EventArgs e) { using (var client = new HttpClient()) { using (var content = new MultipartFormDataContent()) { var rcontent = st

我试图通过API REST将任何文件类型的文档发布到GLPI服务器

以下是我正在做的:

private void button11_Click(object sender, EventArgs e)
{
    using (var client = new HttpClient())
    {
        using (var content = new MultipartFormDataContent())
        {
            var rcontent = string.Empty;
            // HEADERS (URL + Access Tokens)

            //string _ContentType = "multipart/form-data";
            string _Uri = Properties.Settings.Default.GLPI_URL + "/Document/";

            client.BaseAddress = new Uri(_Uri);
            //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(_ContentType));
            client.DefaultRequestHeaders.Add("Session-Token", Properties.Settings.Default.GLPI_SESSION_TOKEN);
            client.DefaultRequestHeaders.Add("App-Token", Properties.Settings.Default.GLPI_APP_TOKEN);

            // JSON Content (input string array with file uploaded informations)

            JSON_C.DocumentAdder JSONContent = new JSON_C.DocumentAdder();
            JSONContent.name = "sth";
            JSONContent._filename = filebytes;
            HttpContent _JSONContent = new StringContent("uploadManifest={\"input\": " + JsonConvert.SerializeObject(JSONContent).ToString() + "}", Encoding.UTF8, "application/json");
            content.Add(_JSONContent);

            // File Content in bytes
            var fileContent = new ByteArrayContent(filebytes);
            fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("_filename") { FileName = filepath };
            //fileContent.ReadAsByteArrayAsync();
            content.Add(fileContent);

            // Request
            HttpResponseMessage reponse;
            var _Method = new HttpMethod("POST");
            reponse = client.PostAsync(_Uri, content).Result;

            // Request response
            rcontent = reponse.Content.ReadAsStringAsync().Result;

            textBox2.Text = reponse.ToString() + Environment.NewLine + rcontent.ToString();
        }
    }
}
但我得到的回应是:

状态代码:400,原因短语:“错误请求”,版本:1.1,内容:System.Net.Http.StreamContent,标题: { 连接:关闭 缓存控制:无存储,必须重新验证,无缓存 日期:2018年11月26日星期一12:50:09 GMT 服务器:Apache/2.4.29 服务器:Ubuntu 内容长度:61 内容类型:application/json;字符集=UTF-8 到期日期:1997年7月26日星期一05:00:00 GM }

与:

[错误\上传\文件\太大\发布\最大\大小,文件似乎太大]

我要上传的文件是592字节!一个请求中的最大总限制为2Mo。php.ini中的post_max_size是8M,这与我将其更改为0后的结果相同,完全没有限制。然后将其设置为20M以匹配upload\u max\u filesize/etc/php/7.2/apache2/php.ini。
上载\u最大\u文件大小\u。。如果有人找到这篇文章需要帮助,下面是我成功的方法: 分别创建会话令牌并使用RestSharp之后

// Upload

var RSClient = new RestClient(Properties.Settings.Default.GLPI_URL);

var request = new RestRequest("Document", Method.POST);
request.AddHeader("Session-Token", Properties.Settings.Default.GLPI_SESSION_TOKEN);
request.AddHeader("App-Token", Properties.Settings.Default.GLPI_APP_TOKEN);
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "multipart/form-data");
request.AddQueryParameter("uploadManifest", "{\"input\": {\"name\": \"UploadFileTest\", \"_filename\": \"GiletsJaunes.jpg\"}}");
request.AddFile("test", @"C:\path\to\File.jpg");

IRestResponse response = RSClient.Execute(request);
var content = response.Content;

textBox2.Text = textBox2.Text + Environment.NewLine + content;
详情:

由于某些原因,我无法使用RestSharp.Authenticator=new SimpleAuthenticator,因此我使用AddHeader添加了这些Auth参数。 由于AddQueryParameter的原因,我无法在新的StringContent中使用序列化Json字符串,所以我手动编写了它。 你好