最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

UnityWebRequest.Post()multipartform

IT培训 admin 9浏览 0评论

UnityWebRequest.Post()multipart / form

我想将图像上传到Nodejs后端服务器(multer插件处理multipart部分)

我的代码是这样的:

IEnumerator Uploadimage(string url, string _headerKey, string _headerValue)
{

    _headerValue = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1YzBiNjgyOTc4YzBiMjMxYjQ1NGVkNmUiLCJpYXQiOjE1NDQyNTE0NTN9.tAFVjpAQMoWrY2d7-0hHQ3KPidHgRmBPcAEL4Pr203Y";
    List<IMultipartFormSection> form = new List<IMultipartFormSection>();


    Texture2D texture = new Texture2D(2, 2);

    string dirPath = Application.persistentDataPath + "/saveImages/";
    System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(dirPath);
    System.IO.FileInfo[] fileInfo = info.GetFiles();
    if (fileInfo[fileInfo.Length - 1].Extension.ToString() == ".jpg" && fileInfo[fileInfo.Length - 1].Name.ToString().Substring(0, 5) == "Image")
    {
        byte[] byteArray = System.IO.File.ReadAllBytes(dirPath + fileInfo[fileInfo.Length - 1].Name.ToString());
        texture.LoadImage(byteArray);
        Debug.Log("111" + dirPath + fileInfo[fileInfo.Length - 1].Name.ToString());
       // string result = Convert.ToBase64String(byteArray);
    }


    // generate a boundary then convert the form to byte[]
    byte[] boundary = UnityWebRequest.GenerateBoundary();
    string contentTypeString = "";
    contentTypeString = "multipart/form-data; boundary=" + System.Text.Encoding.UTF8.GetString(boundary);

    // read a file and add it to the form
    Debug.Log("ssss");
    form.Add(new MultipartFormDataSection("avatar33", "myData"));
    form.Add(new MultipartFormFileSection("avatar", ImageConversion.EncodeToJPG(texture), "test.jpeg", "image/jpeg"));
    UnityWebRequest www = UnityWebRequest.Post(url, form);

    byte[] formSections = UnityWebRequest.SerializeFormSections(form, boundary);
    byte[] terminate = System.Text.Encoding.UTF8.GetBytes(String.Concat("\r\n--", System.Text.Encoding.UTF8.GetString(boundary), "--"));
    // Make my complete body from the two byte arrays
    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/form-data; boundary=", System.Text.Encoding.UTF8.GetString(boundary));


    // Make my request object and add the raw body. Set anything else you need here
    //www.uploadHandler = new UploadHandlerRaw(body);
    //www.uploadHandler.contentType = contentTypeString;

    UploadHandler uploader = new UploadHandlerRaw(body);
    uploader.contentType = contentType;
    www.uploadHandler = uploader;



    //set jwt
    if (_headerKey != null)
        www.SetRequestHeader(_headerKey, _headerValue);


 //   www.SetRequestHeader("Content-Type", "multipart/form-data");


    yield return www.SendWebRequest();
    Debug.Log("request.GetResponseHeader " + www);
    Debug.Log("request.GetResponseHeader " + www.responseCode);
}

在头部JWT实现(它不重要)......

我把调试放在我读取图像的地方并且存在......

我做其他帖子说的所有方式,但没有一个工作....

我看到的是,如果我成功使用postman fileuploaded(我在本地运行服务器)

-------------------------------------------------- ---代码略有改变但仍然没有成功

IEnumerator Uploadimage(string url, string _headerKey, string _headerValue)
    {
        
        _headerValue = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1YzBiNjgyOTc4YzBiMjMxYjQ1NGVkNmUiLCJpYXQiOjE1NDQyNTE0NTN9.tAFVjpAQMoWrY2d7-0hHQ3KPidHgRmBPcAEL4Pr203Y";
        List<IMultipartFormSection> form = new List<IMultipartFormSection>();


        Texture2D texture = new Texture2D(2, 2);

        string dirPath = Application.persistentDataPath + "/saveImages/";
        System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(dirPath);
        System.IO.FileInfo[] fileInfo = info.GetFiles();
        if (fileInfo[fileInfo.Length - 1].Extension.ToString() == ".png" && fileInfo[fileInfo.Length - 1].Name.ToString().Substring(0, 5) == "Image")
        {
            byte[] byteArray = System.IO.File.ReadAllBytes(dirPath + fileInfo[fileInfo.Length - 1].Name.ToString());
            texture.LoadImage(byteArray);
           
        }

        // generate a boundary then convert the form to byte[]
        byte[] boundary = UnityWebRequest.GenerateBoundary();
        string contentTypeString = "";
        contentTypeString = "multipart/form-data; boundary=" + System.Text.Encoding.UTF8.GetString(boundary);

        // read a file and add it to the form
        form.Add(new MultipartFormDataSection("avatar33", "myData"));
        form.Add(new MultipartFormFileSection("avatar", ImageConversion.EncodeToJPG(texture), "test.jpeg", "image/jpeg"));


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

        //set jwt
        if (_headerKey != null)
            www.SetRequestHeader(_headerKey, _headerValue);


        www.SetRequestHeader("Content-Type", "multipart/form-data");


        yield return www.SendWebRequest();
        Debug.Log("request.GetResponseHeader " + www);
        Debug.Log("request.GetResponseHeader " + www.responseCode);
    }
回答如下:

好的我找到了解决方案

1.你应该只是产生边界并在结尾处放置一个终止边界:第一篇文章显示了这一点

2.然后应该解决的最棘手的问题是将form.data字节转换为字符串并将content-disposition:file替换为content-disposition:form-data

现在它适用于IMultipartFormSection

UnityWebRequest.Post()multipart / form

我想将图像上传到Nodejs后端服务器(multer插件处理multipart部分)

我的代码是这样的:

IEnumerator Uploadimage(string url, string _headerKey, string _headerValue)
{

    _headerValue = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1YzBiNjgyOTc4YzBiMjMxYjQ1NGVkNmUiLCJpYXQiOjE1NDQyNTE0NTN9.tAFVjpAQMoWrY2d7-0hHQ3KPidHgRmBPcAEL4Pr203Y";
    List<IMultipartFormSection> form = new List<IMultipartFormSection>();


    Texture2D texture = new Texture2D(2, 2);

    string dirPath = Application.persistentDataPath + "/saveImages/";
    System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(dirPath);
    System.IO.FileInfo[] fileInfo = info.GetFiles();
    if (fileInfo[fileInfo.Length - 1].Extension.ToString() == ".jpg" && fileInfo[fileInfo.Length - 1].Name.ToString().Substring(0, 5) == "Image")
    {
        byte[] byteArray = System.IO.File.ReadAllBytes(dirPath + fileInfo[fileInfo.Length - 1].Name.ToString());
        texture.LoadImage(byteArray);
        Debug.Log("111" + dirPath + fileInfo[fileInfo.Length - 1].Name.ToString());
       // string result = Convert.ToBase64String(byteArray);
    }


    // generate a boundary then convert the form to byte[]
    byte[] boundary = UnityWebRequest.GenerateBoundary();
    string contentTypeString = "";
    contentTypeString = "multipart/form-data; boundary=" + System.Text.Encoding.UTF8.GetString(boundary);

    // read a file and add it to the form
    Debug.Log("ssss");
    form.Add(new MultipartFormDataSection("avatar33", "myData"));
    form.Add(new MultipartFormFileSection("avatar", ImageConversion.EncodeToJPG(texture), "test.jpeg", "image/jpeg"));
    UnityWebRequest www = UnityWebRequest.Post(url, form);

    byte[] formSections = UnityWebRequest.SerializeFormSections(form, boundary);
    byte[] terminate = System.Text.Encoding.UTF8.GetBytes(String.Concat("\r\n--", System.Text.Encoding.UTF8.GetString(boundary), "--"));
    // Make my complete body from the two byte arrays
    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/form-data; boundary=", System.Text.Encoding.UTF8.GetString(boundary));


    // Make my request object and add the raw body. Set anything else you need here
    //www.uploadHandler = new UploadHandlerRaw(body);
    //www.uploadHandler.contentType = contentTypeString;

    UploadHandler uploader = new UploadHandlerRaw(body);
    uploader.contentType = contentType;
    www.uploadHandler = uploader;



    //set jwt
    if (_headerKey != null)
        www.SetRequestHeader(_headerKey, _headerValue);


 //   www.SetRequestHeader("Content-Type", "multipart/form-data");


    yield return www.SendWebRequest();
    Debug.Log("request.GetResponseHeader " + www);
    Debug.Log("request.GetResponseHeader " + www.responseCode);
}

在头部JWT实现(它不重要)......

我把调试放在我读取图像的地方并且存在......

我做其他帖子说的所有方式,但没有一个工作....

我看到的是,如果我成功使用postman fileuploaded(我在本地运行服务器)

-------------------------------------------------- ---代码略有改变但仍然没有成功

IEnumerator Uploadimage(string url, string _headerKey, string _headerValue)
    {
        
        _headerValue = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1YzBiNjgyOTc4YzBiMjMxYjQ1NGVkNmUiLCJpYXQiOjE1NDQyNTE0NTN9.tAFVjpAQMoWrY2d7-0hHQ3KPidHgRmBPcAEL4Pr203Y";
        List<IMultipartFormSection> form = new List<IMultipartFormSection>();


        Texture2D texture = new Texture2D(2, 2);

        string dirPath = Application.persistentDataPath + "/saveImages/";
        System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(dirPath);
        System.IO.FileInfo[] fileInfo = info.GetFiles();
        if (fileInfo[fileInfo.Length - 1].Extension.ToString() == ".png" && fileInfo[fileInfo.Length - 1].Name.ToString().Substring(0, 5) == "Image")
        {
            byte[] byteArray = System.IO.File.ReadAllBytes(dirPath + fileInfo[fileInfo.Length - 1].Name.ToString());
            texture.LoadImage(byteArray);
           
        }

        // generate a boundary then convert the form to byte[]
        byte[] boundary = UnityWebRequest.GenerateBoundary();
        string contentTypeString = "";
        contentTypeString = "multipart/form-data; boundary=" + System.Text.Encoding.UTF8.GetString(boundary);

        // read a file and add it to the form
        form.Add(new MultipartFormDataSection("avatar33", "myData"));
        form.Add(new MultipartFormFileSection("avatar", ImageConversion.EncodeToJPG(texture), "test.jpeg", "image/jpeg"));


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

        //set jwt
        if (_headerKey != null)
            www.SetRequestHeader(_headerKey, _headerValue);


        www.SetRequestHeader("Content-Type", "multipart/form-data");


        yield return www.SendWebRequest();
        Debug.Log("request.GetResponseHeader " + www);
        Debug.Log("request.GetResponseHeader " + www.responseCode);
    }
回答如下:

好的我找到了解决方案

1.你应该只是产生边界并在结尾处放置一个终止边界:第一篇文章显示了这一点

2.然后应该解决的最棘手的问题是将form.data字节转换为字符串并将content-disposition:file替换为content-disposition:form-data

现在它适用于IMultipartFormSection

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论