简体   繁体   English

使用 .NET 将图像上传到 BIM 360 项目

[英]Uploading an image to a BIM 360 project using .NET

How do you upload an image to a BIM 360 Project using .NET?如何使用 .NET 将图像上传到 BIM 360 项目? I have successfully been able to upload an image to a BIM 360 Project using Postman but when using the same code to perform the same function I get an error message saying:我已经成功地使用 Postman 将图像上传到 BIM 360 项目,但是当使用相同的代码执行相同的功能时,我收到一条错误消息:

"{\\"code\\":1001,\\"message\\":\\"image has contents that are not what they are reported to be; image_content_type is invalid; \\"}"

The code I used inside of .NET is the following:我在 .NET 中使用的代码如下:

var client3 = new RestClient("https://developer.api.autodesk.com/hq/v1/accounts/" + accountId + "/projects/" + targetProject + "/image");
        var request3 = new RestRequest(Method.PATCH);
        request3.AddHeader("cache-control", "no-cache");
        request3.AddHeader("Authorization", "Bearer " + bearer.access_token);
        request3.AddHeader("Content-Type", "multipart/form-data");
        request3.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");            
        request3.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", 
            "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"chunk\"; filename=\"C:\\Users\\Nathan\\Desktop\\logo.png\"\r\n" +
            "Content-Type: image/png\r\n\r\n\r\n" +
            "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n" +
            "Content-Disposition: form-data; name=\"type\"\r\n\r\nimage/png\r\n" +
            "------WebKitFormBoundary7MA4YWxkTrZu0gW--",
            ParameterType.RequestBody);
        IRestResponse response3 = client3.Execute(request3);
        string updatedProjectImage = response3.Content;

I used the source code here as a reference to create a PrepareRequest() function that performs all my requests.我使用这里的源代码作为参考来创建一个PrepareRequest()函数来执行我的所有请求。 The parameters I used to complete the function are as follows:我用来完成功能的参数如下:

string updatedProjectImage = PrepareRequest(
    "https://developer.api.autodesk.com/hq/v1/accounts/" + accountId + "/projects/" + targetProjectId + "/image",
    Method.PATCH,
    new Dictionary<string, string>(),
    null,
    headerParams,
    formParams,
    fileParams,
    new Dictionary<string, string>(),
    "multipart/form-data");

headerParams: "Authorization", "Bearer " + bearer.access_token headerParams: "Authorization", "Bearer " + bearer.access_token

formParams: "type", "image/png" (Format can change depending on the image) formParams: "type", "image/png" (格式可以根据图片改变)

fileParams: "file", fileParam "file", fileParam参数: "file", fileParam

fileParam:文件参数:

FileParameter fileParam = FileParameter.Create(
"chunk", 
GetBytesFromFile(fileName), 
Path.GetFileName(fileName), 
"multipart/form-data");`

(A reference to how the GetBytesFromFile() function was made can be found in the source code. Lastly fileName includes the full address path). (可以在源代码中找到有关如何创建 GetBytesFromFile() 函数的参考。最后 fileName 包括完整地址路径)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM