HttpClient, PostAsync, File upload, boundary added to file body
我是该论坛的新手,所以不允许我直接添加图片。我也不允许添加标签" PostAsync"或" MultipartFormDataContent"。
希望该网址的工作以及该帖子有意义!
我正在.NET控制台应用程序中使用HttpClient将文件发布到外部Web服务。
目前,我正在使用的伪文档(.txt)如下所示:
原始文件
POST可以工作并上载文档,但是它已将边界,内容处置和内容类型附加到文档文本中:
发布后的文件
代码如下:
代码
如果我查看RequestMessage-> Content,则标题对我来说看起来不错:Content Header
我的问题是:
这与我的POST有关,还是与Web服务处理/接收请求的方式有关?我尝试了4种不同的"变体"(几乎完全相同)来执行具有相同结果的POST。
我还尝试从边界中删除引号:
1 2 3 | var boundaryValue = content.Headers.ContentType.Parameters.FirstOrDefault(p => p.Name =="boundary"); boundaryValue.Value = boundaryValue.Value.Replace(""", String.Empty); |
感谢任何输入,这已经困扰了我一阵子:)
编辑
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | string filePath = @"C:\\Development\\Customer\\PKH\\Documaster\\TestFileToUpload.txt"; // we need to send a request with multipart/form-data var content = new MultipartFormDataContent(); var fileContent = new StreamContent(File.OpenRead(filePath)); fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { Name =""files"", FileName =""" + Path.GetFileName(filePath) +""" }; fileContent.Headers.ContentType = new MediaTypeHeaderValue("plain/text"); content.Add(fileContent); //Create uri var builder = new UriBuilder(httpClient.BaseAddress +"integration/hub/v1/upload-single-file"); var query = HttpUtility.ParseQueryString(builder.Query); var fileName = Path.GetFileName(filePath); query["file-name"] = fileName; builder.Query = query.ToString(); string url = builder.ToString(); //Do post try { HttpResponseMessage responseString = await httpClient.PostAsync(url, content); var contents = await responseString.Content.ReadAsStringAsync(); JObject newDocument = JObject.Parse(contents); return newDocument; } catch(Exception) { return null; } |
// Eivind
没关系,我最后想出了一个:
接收端希望获得一个八位字节流,而不是多格式/数据,因此我只需要将其作为ByteArray发送并指定MediaTypeHeaderValue即可:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | string filePath = @"C:\\Development\\Customer\\PKH\\Documaster\\TestDocxToUpload.docx"; var filebytes = File.ReadAllBytes(filePath); //Create uri var builder = new UriBuilder(httpClient.BaseAddress +"integration/hub/v1/upload-single-file"); var query = HttpUtility.ParseQueryString(builder.Query); var fileName = Path.GetFileName(filePath); query["file-name"] = fileName; builder.Query = query.ToString(); string url = builder.ToString(); //Do post try { using (var content = new ByteArrayContent(filebytes)) { content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); HttpResponseMessage responseString = await httpClient.PostAsync(url, content); var contents = await responseString.Content.ReadAsStringAsync(); JObject newDocument = JObject.Parse(contents); return newDocument; } } catch(Exception) { return null; } |
// Eivind
非常感谢您的帖子。我一直在寻找一种使用Graph上传文档的方法,但找不到用于显示Word文档内容格式的示例。你真的帮助了我。
这就是我使用它通过Graph将Word文档上载到SharePoint在线网站的方式。
1 2 3 4 5 6 7 8 9 | byte[] fileContent = File.ReadAllBytes("Samples\\\\Document1.docx"); string targetPath ="/UploadedDocument.docx"; string uploadUrl = $"https://graph.microsoft.com/v1.0/sites/{siteId}/drive/root:{targetPath}:/content"; using (var content = new ByteArrayContent(fileContent)) { content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream"); HttpResponseMessage responseString = await httpClient.PutAsync(uploadUrl, content); string contents = await responseString.Content.ReadAsStringAsync(); } |