简体   繁体   English

如何通过Flurl MultipartFormDataContent将附件发布到BIM 360字段

[英]How to POST an attachment to BIM 360 Field via Flurl MultipartFormDataContent

I'm trying to upload an attachment to an issue in BIM 360 Field by an application (.NET framework). 我正在尝试通过应用程序(.NET Framework)上传BIM 360 Field中问题的附件。 I'm using following endpoint (BIM 360 Field API Doc: https://bim360field.autodesk.com/apidoc/index.html#mobile_api_method_21 ). 我正在使用以下端点(BIM 360 Field API文档: https : //bim360field.autodesk.com/apidoc/index.html#mobile_api_method_21 )。

To build the request I'm using Flurl and MultipartFormDataContent (see code below). 为了建立请求,我使用FlurlMultipartFormDataContent (请参见下面的代码)。 However I get a 500 Internal Server Error back and unfortunately there is no more specific information what exactly went wrong. 但是,我得到了500 Internal Server Error的提示 ,但是不幸的是,没有更多的具体信息究竟出了什么问题。

I tried to upload an attachment with exactly the same url, ticked, project_id and 'attachment detail string' via Postman and it worked fine. 我尝试通过邮递员上传具有完全相同的url,打勾,project_id和“附件详细信息字符串”的附件 ,并且效果很好。

Code: 码:

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);

I expect there is a problem with my MultipartFormDataContent . 我希望我的MultipartFormDataContent有问题。 Am I missing something? 我想念什么吗?

Best regards 最好的祝福

Chris 克里斯

It seems to be an issue of the Flurl library ( ref ) and be always failed to post the right content. 这似乎是Flurl库( ref )的问题,并且始终无法发布正确的内容。 Anyway, here is the working code snippet for playing with Flurl. 无论如何,这是使用Flurl的工作代码段。 Hope it helps! 希望能帮助到你!

BTW, 顺便说一句,

this API to part of the classical BIM360 Field, and it's not part of the Forge platform. 该API是经典BIM360领域的一部分,而不是Forge平台的一部分。 Therefore, please post your questions to https://forums.autodesk.com/t5/bim-360-api-forum/bd-p/115 , Thanks! 因此,请将您的问题发布到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;
        }

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

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