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

时间:2019-01-18 16:11:00

标签: c# .net asp.net-web-api restsharp

如何使用.NET将图像上传到BIM 360项目?我已经能够使用Postman成功地将图像上传到BIM 360 Project,但是当使用相同的代码执行相同的功能时,我收到一条错误消息,提示:

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

我在.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;

1 个答案:

答案 0 :(得分:1)

我在这里使用源code作为创建执行我所有请求的PrepareRequest()函数的参考。我用来完成该功能的参数如下:

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

formParams:"type", "image/png"(格式可以根据图像而改变)

fileParams:"file", fileParam

fileParam:

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

(有关如何获取GetBytesFromFile()函数的信息,请参见源代码。最后,fileName包含完整的地址路径。)