0

我正在尝试通过应用程序(.NET 框架)上传BIM 360 Field中问题的附件。我正在使用以下端点(BIM 360 Field API Doc:https ://bim360field.autodesk.com/apidoc/index.html#mobile_api_method_21 )。

为了构建请求,我使用了 FlurlMultipartFormDataContent(参见下面的代码)。但是,我收到了500 Internal Server Error,不幸的是,没有更具体的信息究竟出了什么问题。

我尝试通过Postman上传具有完全相同的url、勾选、project_id 和“附件详细信息字符串”的附件,它工作正常。

代码:

byte[] fileByteArray = memoryStream.ToArray();
string attachment = JsonConvert.SerializeObject(attachmentDetails);

MultipartFormDataContent conntent = new MultipartFormDataContent();
conntent.Add(new ByteArrayContent(fileByteArray, 0, fileByteArray.Length), "original");
conntent.Add(new StringContent(attachment), "attachment");

FlurlClient client = fieldConnection.GetClient("attachments");
client.Url.SetQueryParam("ticket", Ticket);
client.Url.SetQueryParam("project_id", project);

return await client
             .WithTimeout(30)
             .SendAsync(HttpMethod.Post, conntent)
             .ConfigureAwait(false);

我预计我的MultipartFormDataContent有问题。我错过了什么吗?

此致

克里斯

4

1 回答 1

1

这似乎是 Flurl 库(ref)的问题,并且总是无法发布正确的内容。无论如何,这里是玩 Flurl 的工作代码片段。希望能帮助到你!

顺便提一句,

这个 API 是经典 BIM360 Field 的一部分,它不是 Forge 平台的一部分。因此,请将您的问题发布到https://forums.autodesk.com/t5/bim-360-api-forum/bd-p/115,谢谢!

 public static HttpResponseMessage AttachmentsByFlurl(string ticket, string project_id,
            string originalPath, string thumbPath,
            string container_id, string container_type)
        {
            // Compose Attachment JSON string 
            FileInfo original = new FileInfo(originalPath);
            Dictionary<string, string> att = new Dictionary<string, string>();

            // date time format: "2015-08-05 15:28:17 -0500";
            string dateTimeFormat = "yyyy-MM-dd HH:mm:ss zzz";
            string curTime = DateTime.Now.ToString(dateTimeFormat);
            att["fcreate_date"] = original.CreationTime.ToString(dateTimeFormat);
            att["fmod_date"] = original.LastWriteTime.ToString(dateTimeFormat);
            att["created_at"] = curTime;
            att["updated_at"] = curTime;

            att["size"] = original.Length.ToString();
            att["content_type"] = MimeMapping.GetMimeMapping(original.Name);
            att["filename"] = original.Name;
            att["container_id"] = container_id;     // e.g., issue_id 
            att["container_type"] = container_type; // e.g., "Issue" 

            // Conver to JSON format
            string attachment = Newtonsoft.Json.JsonConvert.SerializeObject(att);

            var mpc = new MultipartContent();
            var ticketContent = new StringContent(ticket);
            ticketContent.Headers.Add("Content-Disposition", "form-data; name=\"ticket\"");
            mpc.Add(ticketContent);

            var projectIdContent = new StringContent(project_id);
            projectIdContent.Headers.Add("Content-Disposition", "form-data; name=\"project_id\"");
            mpc.Add(projectIdContent);

            var attachmentContent = new StringContent(attachment, Encoding.UTF8, "application/json");
            attachmentContent.Headers.Add("Content-Disposition", "form-data; name=\"attachment\"");
            mpc.Add(attachmentContent);

            var attachmentFileStream = File.OpenRead(originalPath);
            var attachmentContentStream = new StreamContent(attachmentFileStream);
            attachmentContentStream.Headers.Add("Content-Disposition", string.Format("form-data; name=\"original\"; filename=\"{0}\"", Path.GetFileName(originalPath)));
            mpc.Add(attachmentContentStream);

            if(!string.IsNullOrEmpty(thumbPath))
            {
                var thumbFileStream = File.OpenRead(thumbPath);
                var thumbContentStream = new StreamContent(thumbFileStream);
                thumbContentStream.Headers.Add("Content-Disposition", string.Format("form-data; name=\"thumb\"; filename=\"{0}\"", Path.GetFileName(thumbPath)));
                mpc.Add(thumbContentStream);
            }

            var url = "https://bim360field.autodesk.com/api/attachments";
            var resp = url
                        .PostAsync(mpc)
                        .Result;

            return resp;
        }
于 2019-06-06T06:28:17.047 回答