Recap API 从磁盘发送文件

Recap API send files from disk

我正在尝试使用 Recap API.

在 C# 中复制将文件(从磁盘)发送到照片场景的 curl 请求
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 上的文件名作为第三个参数发送.