当前位置: 首页 > 知识库问答 >
问题:

Google Drive API上载HTTP多部分单元3D

马丰
2023-03-14

我有这种代码

List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
formData.Add(new MultipartFormDataSection("metadata","{ \"name\":" + salvataggio2.Substring(0, salvataggio2.Length - 5) + "-to-google-drive.json" + ", \"parents\":['" + entries.Files[0].Id + "'] }","application/json"));
formData.Add(new MultipartFormDataSection("file", Application.persistentDataPath + "/Saves/" + salvataggio2, "application/json"));
//formData.Add(new MultipartFormFileSection(salvataggio2.Substring(0, salvataggio2.Length - 5) + "-to-google-drive.json", Application.persistentDataPath + "/Saves/" + salvataggio2));
//string jsonMetadata = "--foo_bar_baz" + System.Environment.NewLine + " Content-Type: application/json; charset=UTF-8 { \"name\":" + salvataggio2.Substring(0, salvataggio2.Length - 5) + "-to-google-drive.json" + ", \"parents\":['" + entries.Files[0].Id + "'] }" + System.Environment.NewLine + "--foo_bar_baz" + System.Environment.NewLine + "Content-Type: application/json; charset=UTF-8" + File.ReadAllText(Application.persistentDataPath + "/Saves/" + salvataggio2) + System.Environment.NewLine + "--foo_bar_baz--" + System.Environment.NewLine;
//byte[] bytes = Encoding.UTF8.GetBytes(jsonMetadata);
using (UnityWebRequest www = UnityWebRequest.Post("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart", formData)) {
//var upload = new UploadHandlerRaw(bytes);
//www.uploadHandler = upload;
www.SetRequestHeader("Authorization", "Bearer " + response.Access_token);
//www.SetRequestHeader("Content-Type", "multipart/related; boundary=foo_bar_baz");

yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.ProtocolError) {
    Debug.Log(www.downloadHandler.text);
    this.gameObject.SetActive(false);
} else {
    Debug.Log(www.downloadHandler.text);
}
}

如你所见,我需要上传一个json文件到google Drive。我已经获得父文件夹的文件ID。不知道我做错了什么,但我有多个错误。就像

“code”:400,“Message”:“Parse Error”(用于当前未注释的代码),如果我尝试使用MultipartFormFileSection部分而不是MultipartFormDataSection文件部分,则MultiPartBody格式不正确,如果我尝试使用具有“Content-Type”的jsonMetadata,“multipart/Related;boundary=foo_bar_baz”,则缺少结束边界。

我该如何解决这个问题呢?

编辑。我尝试对当前的jsonMetadata变量(包含所有内容的变量)进行Log操作,更正了一些内容,得到了以下结果

--foo_bar_baz
Content-Disposition: form-data; name="metadata"
Content-Type: application/json; charset=UTF-8

{ "name":"Autosave - Lief-to-google-drive.json", "parents":["1N7pYSBW-eI-sMcS0KF4cjd9IuTYVTani"] }
--foo_bar_baz
Content-Disposition: form-data; name="file"
Content-Type: application/json; charset=UTF-8

{"nickname":"Lief","sex":false,"startingPoint":[0.0,0.0,-5.0],"startingRotation":[0.0,0.0,0.0],"startingCameraPoint":[0.0,0.0,0.0],"startingCameraRotation":[0.0,0.0,0.0],"npcRep":[{"dialogNumber":"00001","rep":0,"side":"default"}],"sideRep":[{"side":"default","rep":0},{"side":"demo","rep":-100}]}
--foo_bar_baz--

我并不是真的没有意识到我需要改变什么,因为我仍然得到“多部分体中缺少末端边界”。

--foo_bar_baz--应该是结尾

我的代码部分现在像这样

string jsonMetadata = "--foo_bar_baz" + System.Environment.NewLine 
            + "Content-Disposition: form-data; name=\"metadata\"" + System.Environment.NewLine 
            + "Content-Type: application/json; charset=UTF-8" + System.Environment.NewLine + System.Environment.NewLine
            + "{ \"name\":\"" + salvataggio2.Substring(0, salvataggio2.Length - 5) + "-to-google-drive.json\"" + ", \"parents\":[\"" + entries.Files[0].Id + "\"] }" + System.Environment.NewLine 
            + "--foo_bar_baz" + System.Environment.NewLine
            + "Content-Disposition: form-data; name=\"file\"" + System.Environment.NewLine
            + "Content-Type: application/json; charset=UTF-8" + System.Environment.NewLine + System.Environment.NewLine
            + File.ReadAllText(Application.persistentDataPath + "/Saves/" + salvataggio2) + System.Environment.NewLine 
            + "--foo_bar_baz--" + System.Environment.NewLine;

共有1个答案

钦侯林
2023-03-14

好的,我终于解决了这个问题(我可以说的是,我现在不确定它是如何工作的,但我可以肯定地说,我需要用Unity api创建边界)

        List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
        formData.Add(new MultipartFormDataSection("metadata", "{ \"name\":\"" + salvataggio2.Substring(0, salvataggio2.Length - 5) + "-to-google-drive.json\"" + ", \"parents\":[\"" + entries.Files[0].Id + "\"] }", "application/json"));
        formData.Add(new MultipartFormDataSection("file", File.ReadAllText(Application.persistentDataPath + "/Saves/" + salvataggio2), "application/json"));
        byte[] boundary = UnityWebRequest.GenerateBoundary();
        byte[] formSections = UnityWebRequest.SerializeFormSections(formData, boundary);
        byte[] terminate = Encoding.UTF8.GetBytes(String.Concat("\r\n--", Encoding.UTF8.GetString(boundary), "--"));
        byte[] body = new byte[formSections.Length + terminate.Length];
        Buffer.BlockCopy(formSections, 0, body, 0, formSections.Length);
        Buffer.BlockCopy(terminate, 0, body, formSections.Length, terminate.Length);
        string contentType = String.Concat("multipart/related; boundary=", Encoding.UTF8.GetString(boundary));

        using (UnityWebRequest www = UnityWebRequest.Post("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart", "")) {
            var upload = new UploadHandlerRaw(body);
            www.uploadHandler = upload;
            www.SetRequestHeader("Authorization", "Bearer " + response.Access_token);
            www.SetRequestHeader("Content-Type", contentType);

            yield return www.SendWebRequest();
            if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.ProtocolError) {
                Debug.Log(www.downloadHandler.text);
                this.gameObject.SetActive(false);
            } else {
                Debug.Log(www.downloadHandler.text);
            }
        }

正如您所看到的,在前2个添加到formData之后(与前面相同),它创建了一个随机边界,它序列化了表单和边界,它创建了一个终止,它融合了所有内容,并且它创建了一个兼容的内容类型。在此之后,我只需创建通常的Unity Post(在正文中为字节数组使用UploadHandler)就完成了。

 类似资料:
  • 问题内容: 我有一个代码库,当前使用Post上传文件,并具有enctype作为multipart / form- data。现在,我需要包括一些表单项,即一些参数也将随文件上传一起传递。我已经创建了html表单,但是我不能使用request.getParameter,因为它是一个多部分表单。谁能建议我如何将参数与上传文件一起传递。我提供以下代码。请根据我的代码的兼容性建议我如何解决 问题答案: 如

  • 我正在尝试使用Support Bee API创建附件,如下所述:https://supportbee.com/api#create_attachment 我编写了一个服务,它使用创建并发送使用文件名的请求。 如果我在《邮递员》中测试,它会成功。我正在为正文使用,只是从UI中选择要上载的文件: 当我试图通过我的服务上传它时,它不起作用: 这将导致500内部服务器错误。检查对象时,我可以看到它的标题值

  • Im使用Spring Boot,并希望使用控制器接收多部分文件上传。当发送文件时,我一直得到错误415不支持的内容类型响应,并且控制器从未到达 我尝试在HTML/JSP页面和使用RESTTemplate的独立客户端应用程序中使用Form:Action发送。所有尝试的结果都是一样的 从multipart文档看来,边界参数必须添加到multipart上载,但这似乎与控制器接收不匹配 我的控制器方法设置

  • 关于Spring MVC应用程序中的多部分文件上传问题,我已经看到了很多关于stackoverflow的答案。我一步一步地确保我不会重复别人犯过的错误。 这是我的表格 在pom文件中我有依赖项 因此,当我停在调试器中时,request对象显示文件已被接收,甚至存储在jBoss临时文件夹中,我并不感到惊讶。 你能指出Spring看不到上传的文件我会错过什么吗?

  • 我正在使用AWS成功地进行身份验证,并在Bucket对象上使用“put_object”方法上载文件。现在,我想使用多部分API来完成对大型文件的访问。我在这个问题中找到了公认的答案:如何使用boto3将S3对象保存到文件中 但是当试图实现时,我得到了“未知的方法”错误。我做错了什么?我的代码在下面。谢谢!

  • The Play framework is built using Akka, and is well suited for building both full web applications as well as REST services. The Spray toolkit is built using Akka, and is a minimalistic HTTP/REST laye