public override HttpRequestMessage GetCustomizedRequest(Uri serverUri) { string boundary = "Upload---------" + Guid.NewGuid().ToString(); var request = new HttpRequestMessage(HttpMethod.Post, UploadUri); foreach (var hi in GetAdditionalHeaders()) { request.Headers.Add(hi.Key, hi.Value); } var content = new MultipartFormDataContentEx(boundary); // Add files to upload to the request foreach (var f in Files) { //var fileContent = new StreamContent(f.FileContent); var fileContent = new ProgressableStreamContent(f.FileContent, (p) => { if (UploadProgress != null) { UploadProgress(p); } }); fileContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); fileContent.Headers.TryAddWithoutValidation("Content-Disposition", String.Format("form-data; name=\"file\"; filename=\"{0}\"", f.Filename)); content.Add(fileContent); } // the parent dir to upload the file to string tDir = TargetDirectory; if (!tDir.StartsWith("/")) { tDir = "/" + tDir; } var dirContent = new StringContent(tDir, Encoding.UTF8); dirContent.Headers.ContentType = null; dirContent.Headers.TryAddWithoutValidation("Content-Disposition", @"form-data; name=""parent_dir"""); content.Add(dirContent); // transmit the content length long conLen; if (!content.ComputeLength(out conLen)) { conLen = 0; } // the seafile-server implementation rejects the content-type if the boundary value is // placed inside quotes which is what HttpClient does, so we have to redefine the content-type without using quotes // and remove the actual content-type which uses quotes beforehand content.Headers.ContentType = null; content.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundary); if (conLen > 0) { // in order to disable buffering // and make the progress work content.Headers.Add("Content-Length", conLen.ToString()); } request.Content = content; return(request); }
//private static Stream BuildMultipartStream(string name, string fileName, byte[] fileBytes, string boundary) //{ // // Create multipart/form-data headers. // byte[] firstBytes = Encoding.UTF8.GetBytes(String.Format( // "--{0}\r\n" + // "Content-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"\r\n" + // "\r\n", // boundary, // name, // fileName)); // byte[] lastBytes = Encoding.UTF8.GetBytes(String.Format( // "\r\n" + // "--{0}--\r\n", // boundary)); // int contentLength = firstBytes.Length + fileBytes.Length + lastBytes.Length; // byte[] contentBytes = new byte[contentLength]; // // Join the 3 arrays into 1. // Array.Copy( // firstBytes, // 0, // contentBytes, // 0, // firstBytes.Length); // Array.Copy( // fileBytes, // 0, // contentBytes, // firstBytes.Length, // fileBytes.Length); // Array.Copy( // lastBytes, // 0, // contentBytes, // firstBytes.Length + fileBytes.Length, // lastBytes.Length); // return new MemoryStream(contentBytes); //} public override HttpRequestMessage GetCustomizedRequest(Uri serverUri) { string boundary = "Upload---------" + Guid.NewGuid().ToString(); var request = new HttpRequestMessage(HttpMethod.Post, UploadUri); foreach (var hi in GetAdditionalHeaders()) { request.Headers.Add(hi.Key, hi.Value); } var content = new MultipartFormDataContentEx(boundary); // Add files to upload to the request foreach (var f in Files) { //var fileContent = new StreamContent(f.FileContent); var fileContent = new ProgressableStreamContent(f.FileContent, (p) => { if (UploadProgress != null) { UploadProgress(p); } }); fileContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); fileContent.Headers.ContentType.CharSet = "\'utf8\'"; string fileName = f.Filename; if (IsGBCode(f.Filename)) { fileName = GB2312ToUtf8(f.Filename); } else if (IsGBKCode(f.Filename)) { fileName = GBKToUtf8(f.Filename); } // fileContent.Headers.ContentType.CharSet = "UTF-8"; //fileContent.Headers.TryAddWithoutValidation("Content-Disposition", String.Format("form-data; name=\"file\"; filename=\"{0}\"", Files[0].Filename)); String headerValue = "form-data; name=\"file\"; filename=\"" + Files[0].Filename + "\""; byte[] bytes = Encoding.UTF8.GetBytes(headerValue); headerValue = ""; foreach (byte b in bytes) { headerValue += (Char)b; } fileContent.Headers.Add("Content-Disposition", headerValue); content.Add(fileContent); } /* * byte[] fileBytes = new byte[Files[0].FileContent.Length]; * files[0].FileContent.Read(fileBytes, 0, (int)Files[0].FileContent.Length); * content.Add(new StreamContent( * BuildMultipartStream("file", Files[0].Filename, fileBytes, boundary))); */ /* * string fileName = Files[0].Filename; * * if (IsGBCode(Files[0].Filename)) * { * fileName = GB2312ToUtf8(Files[0].Filename); * } * else if (IsGBKCode(Files[0].Filename)) * { * fileName = GBKToUtf8(Files[0].Filename); * } * * var streamContent = new StreamContent(Files[0].FileContent); * * streamContent.Headers.Add("Content-Type", "application/octet-stream"); * streamContent.Headers.ContentType.CharSet = "UTF-8"; * streamContent.Headers.Add("Content-Disposition", * "form-data; name=\"file\"; filename=\"" + fileName + "\""); * content.Add(streamContent, "file", fileName); */ // the parent dir to upload the file to string tDir = TargetDirectory; if (!tDir.StartsWith("/")) { tDir = "/" + tDir; } var dirContent = new StringContent(tDir, Encoding.UTF8); dirContent.Headers.ContentType = null; dirContent.Headers.TryAddWithoutValidation("Content-Disposition", @"form-data; name=""parent_dir"""); content.Add(dirContent); // transmit the content length, for this we use the private method TryComputeLength() called by reflection long conLen; if (!content.ComputeLength(out conLen)) { conLen = 0; } // the seafile-server implementation rejects the content-type if the boundary value is // placed inside quotes which is what HttpClient does, so we have to redefine the content-type without using quotes // and remove the actual content-type which uses quotes beforehand content.Headers.ContentType = null; content.Headers.ContentLength = null; content.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundary); //content.Headers.ContentType.CharSet = "UTF-8"; //client.DefaultRequestHeaders.TransferEncodingChunked = true; if (conLen > 0) { // in order to disable buffering // and make the progress work content.Headers.Add("Content-Length", conLen.ToString()); } request.Content = content; return(request); }