如何使用PUT方法Unity3D更新用户图片

问题描述 投票:1回答:1

我在Unity3D初学者;我有开发移动应用程序,我需要管理用户配置文件数据;我必须把这些数据用服务器REST服务进行通信。一切工作正常,当我从我的应用程序发送JSON(如姓名,电子邮件,电话号码等),但我不能更新的资料图片。

我需要的是:内容类型=多部分/格式数据密钥=“profile_picture”,值= file_to_upload(不是路径)

我读了很多关于在统一的网络,并试图UnityWebRequest,列表,WWWform的不同组合,但似乎没有对这种PUT服务的工作。

UnityWebRequest www = new UnityWebRequest(URL + user.email, "PUT");
    www.SetRequestHeader("Content-Type", "multipart/form-data");
    www.SetRequestHeader("AUTHORIZATION", authorization);
    //i think here i'm missing the correct way to set up the content

我可以正确地模拟从邮差更新,所以它不是与服务器有问题;我敢肯定,问题是,我不能转换应用这里面的逻辑。

从邮差上传正常工作(1)

enter image description here

从邮差上传正常工作(2)

enter image description here

任何形式的帮助和代码的建议将不胜感激。谢谢

c# rest unity3d client-server
1个回答
0
投票

随着Put你通常只发送文件数据,但没有一种形式。

您可以使用UnityWebRequest.Post添加多形式

IEnumerator Upload() 
{
    List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
    formData.Add(new MultipartFormFileSection("profile_picture", byte[], "example.png", "image/png"));

    UnityWebRequest www = UnityWebRequest.Post(url, formData);

    // change the method name
    www.method = "PUT"; 

    yield return www.SendWebRequest();

    if(www.error) 
    {
        Debug.Log(www.error);
    }
    else 
    {
        Debug.Log("Form upload complete!");
    }
}

使用MultipartFormFileSection


或者你也可以使用一个WWWForm

IEnumerator Upload()
{
    WWWForm form = new WWWForm();
    form.AddBinaryData("profile_picture", bytes, "filename.png", "image/png");

    // Upload via post request
    var www = UnityWebRequest.Post(screenShotURL, form);

    // change the method name
    www.method = "PUT";        

    yield return www.SendWebRequest();

    if (www.error) 
    {
        Debug.Log(www.error);
    }
    else 
    {
        Debug.Log("Finished Uploading Screenshot");
    }
}

使用WWWForm.AddBinaryData


请注意,对于用户认证,就必须正确编码您的凭据:

string authenticate(string username, string password)
{
    string auth = username + ":" + password;
    auth = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(auth));
    auth = "Basic " + auth;
    return auth;
}

www.SetRequestHeader("AUTHORIZATION", authenticate("user", "password"));

(Qazxswpoi)

© www.soinside.com 2019 - 2024. All rights reserved.