繁体   English   中英

Recap API 从磁盘发送文件

[英]Recap API send files from disk

我正在尝试在 C# 中复制 curl 请求,以使用 Recap API 将文件(从磁盘)发送到 photoscene。

curl -v 'https://developer.api.autodesk.com/photo-to-3d/v1/file' \
-X 'POST' \
-H 'Authorization: Bearer eyjhbGCIOIjIuzI1NiISimtpZCI6...' \
-F "photosceneid=hcYJcrnHUsNSPII9glhVe8lRF6lFXs4NHzGqJ3zdWMU" \
-F "type=image" \
-F "file[0]=@c:/sample_data/_MG_9026.jpg" \
-F "file[1]=@c:/sample_data/_MG_9027.jpg"

到目前为止,我在 C# 中得到了这个

private async Task<string> SendUploadJsonAsync(PhotoSceneImages obj, HttpMethod method, string token)
{
    const string url = "https://developer.api.autodesk.com/photo-to-3d/v1/file";

    using (var client = new HttpClient())
    {
        var formData = new MultipartFormDataContent
        {
            {new StringContent(obj.photosceneid), "photosceneid"}, {new StringContent("type"), "image"}
        };

        var i = 0;
        foreach (var image in obj.files)
        {
            formData.Add(new ByteArrayContent(image), "file["+ i++ +"]");
        }
        var request = new HttpRequestMessage
        {
            Content = formData,
            Headers =
            {
                Authorization = new AuthenticationHeaderValue("Bearer", token)
            },
            Method = method,
            RequestUri = new Uri(url)
        };

        try
        {
            var response = await client.SendAsync(request);
            var result = await response.Content.ReadAsStringAsync();
            return result;
        }
        catch (Exception e)
        {
            return e.Message;
        }
    }
}

但是我收到了来自 Autodesk 服务器的尚未实现的回复。

不知道我是不是做错了什么。

PhotoSceneImages 对象包含一个带有 photosceneid 的字符串和一个包含图像文件字节的字节数组。

搞定了,

private async Task<string> SendUploadImagesAsync(PhotoSceneImages obj, HttpMethod method, string token)
{
    const string url = "https://developer.api.autodesk.com/photo-to-3d/v1/file";

    using (var client = new HttpClient())
    {
        var formData = new MultipartFormDataContent
        {
            {new StringContent(obj.photosceneid), "photosceneid"}, {new StringContent(obj.type), "type"}
        };

        var i = 0;
        foreach (var file in obj.files)
        {
            formData.Add(new ByteArrayContent(file.byteArray), $"file[{i++}]", file.filename);
        }
        var request = new HttpRequestMessage
        {
            Content = formData,
            Headers =
            {
                Authorization = new AuthenticationHeaderValue("Bearer", token)
            },
            Method = method,
            RequestUri = new Uri(url)
        };
        Debug.Log($"request: {request}");
        try
        {
            var response = await client.SendAsync(request);
            var result = await response.Content.ReadAsStringAsync();
            return result;
        }
        catch (Exception e)
        {
            return e.Message;
        }
    }
}

基本上有两件事是错误的,字段 ("type":"image") 我以错误的方式发送它,您还需要将 formData 上的文件名作为第三个参数发送。

暂无
暂无

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

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