我是这个论坛的新手,所以我不允许直接添加图片。我也不允许添加标签“ PostAsync”或“ MultipartFormDataContent”。 希望网址的工作和帖子的意义!
我在.NET控制台应用程序中使用HttpClient将文件发布到外部Web服务。 目前,我正在使用如下所示的虚拟文档(.txt): Original document
POST可以工作并上载文档,但是它已将边界,内容处置和内容类型附加到文档文本中: Document after post
代码如下: code
如果我查看RequestMessage-> Content,则标题对我来说看起来不错:Content Header
我的问题是: 这与我的POST有关,还是与Web服务如何处理/接收请求有关?我已经尝试了4种不同的“变体”(几乎完全相同)来执行具有相同结果的POST。 我还尝试从边界中删除引号:
var boundaryValue = content.Headers.ContentType.Parameters.FirstOrDefault(p => p.Name == "boundary");
boundaryValue.Value = boundaryValue.Value.Replace("\"", String.Empty);
感谢任何输入,这已经困扰了我一阵子:)
编辑
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
答案 0 :(得分:0)
没关系,我最终想出了这个: 接收端希望获得一个八位字节流,而不是多格式/数据,因此我只需要将其作为ByteArray发送并指定MediaTypeHeaderValue即可:
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
答案 1 :(得分:0)
非常感谢您的帖子。我一直在寻找一种使用 Graph 上传文档的方法,但找不到显示如何设置 Word 文档内容格式的示例。你真的帮了我。
这就是我使用 Graph 将 Word 文档上传到 SharePoint 在线网站的方式。
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();
}