MultipartFormDataContent不适用于大于约22个字节的文件

时间:2018-07-16 18:32:26

标签: c# .net file-upload multipartform-data

我正在尝试使用MultipartFormDataContent将文件上传到服务器,但是由于某种原因,它适用于非常小的文件,例如“ 22字节”,但不能更大。因此它适用于很小的文本文件,但不适用于图像或视频。

这是男女同校:

    static Encoding ISO_8859_1 { get { return Encoding.GetEncoding("ISO-8859-1"); } }
    public static void UploadFile(string _uploadEndPoint, string _fileToUpload, string userName, string password)
    {
        using (HttpClient httpClient = new HttpClient())
        {
            string _auth = string.Format("{0}:{1}", userName, password);
            string _enc = Convert.ToBase64String(ISO_8859_1.GetBytes(_auth));
            string _cred = string.Format("{0} {1}", "Basic", _enc);
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _enc);
            httpClient.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("text/html"));
            httpClient.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xhtml+xml"));
            httpClient.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml;q=0.9"));
            httpClient.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("image/webp"));
            httpClient.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("*/*;q=0.8"));
            httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("user-agent");
            httpClient.DefaultRequestHeaders.Upgrade.ParseAdd("1");



            using (MultipartFormDataContent content = new MultipartFormDataContent())
            {
                using (FileStream stream = File.Open(_fileToUpload, FileMode.Open, FileAccess.Read))
                {
                    using (StreamContent streamConent = CreateFileContent(stream, _fileToUpload.Split('\\').Last(), "application/octet-stream"))

                    {
                        content.Add(new StringContent("upload"), "submit");
                        content.Add(new StringContent(""), "directory");
                        content.Add(new StringContent(_fileToUpload.Split('\\').Last()), "filename");
                        content.Add(new StringContent("as_json"), "options");
                        content.Add(streamConent);
                        var taskUpload = httpClient.PostAsync(_uploadEndPoint, content);
                        taskUpload.Wait();
                        if (taskUpload.Status == TaskStatus.RanToCompletion)
                        {
                            Console.WriteLine("File uploaded");
                            Console.WriteLine(taskUpload.Result.ToString());
                        }

                    }
                }
            }
        }
    }



    private static StreamContent CreateFileContent(Stream stream, string fileName, string contentType)
    {
        var fileContent = new StreamContent(stream, 4 * 1024);
        fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
        {
            Name = "\"file\"",
            FileName = "\"" + fileName + "\""
        };
        fileContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);
        return fileContent;
    }

0 个答案:

没有答案