Unity/C# multipart/form-data 提交文件和参数

参考链接:https://blog.csdn.net/qq_34611658/article/details/105530402

 /// <summary>
    /// 上传文件
    /// </summary>
    /// <param name="url"><服务器地址>
    /// <param name="dic"></param>
    /// <param name="filepath"><文件的本地储存地址>
    /// <param name="filename"><文件的名字>
    /// <returns></returns>
    public static string PostJsonData(string url, Dictionary<string, string> dic, string filepath, string filename)
    {
        string str = "";
        try
        {
            HttpClient client = new HttpClient();
            var postContent = new MultipartFormDataContent();
            string boundary = string.Format("--{0}", DateTime.Now.Ticks.ToString("x"));
            postContent.Headers.Add("ContentType", $"multipart/form-data, boundary={boundary}");
            postContent.Headers.Add("unionId", UserInfo.UnionId);
            postContent.Headers.Add("token", UserInfo.User_Token);
            string filekeyname = "file";
            postContent.Add(new ByteArrayContent(File.ReadAllBytes(filepath)), filekeyname, filename);
            foreach (var key in dic.Keys)
            {
                postContent.Add(new StringContent(dic[key].ToString()), key);
            }
            HttpResponseMessage response = client.PostAsync(url, postContent).Result;
            str = response.Content.ReadAsStringAsync().Result;
        }
        catch (Exception ex)
        {
            Debug.LogError("PostJsonData:" + ex.ToString());
        }
        return str;
    }

调用

Dictionary<string, string> keyValues = new Dictionary<string, string>();
keyValues.Add("cert", cert);//cert是服务器返回的凭证  这里的凭证只是针对这个项目后端人员定义的
string result = PostJsonData(url, keyValues, path, filename);

下图是后端给的参数值:
图中参数名称file对应代码中的filekeyname **加粗样式**
添加:截屏上传至服务器
起截图名字的时候记得加后缀(.png/.jpg)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Unity中进行multipart/form-data上传,可以使用UnityWebRequest类。具体步骤如下: 1. 创建一个UnityWebRequest对象,并设置请求的URL和请求方法为POST。 2. 设置请求头部信息,包括Content-Type为multipart/form-data,以及其他必要的信息。 3. 构造表单数据,将需要上传的数据按照multipart/form-data格式进行编码,并设置到UnityWebRequest对象中。 4. 发送请求,并等待服务器响应。 以下是一个示例代码: ``` IEnumerator UploadFile(string url, byte[] data) { UnityWebRequest request = new UnityWebRequest(url, "POST"); request.SetRequestHeader("Content-Type", "multipart/form-data; boundary=------------------------boundary"); string boundary = "--------------------------boundary"; byte[] formHeaderBytes = System.Text.Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"file\"; filename=\"test.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n"); byte[] formFooterBytes = System.Text.Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); byte[] bodyBytes = new byte[formHeaderBytes.Length + data.Length + formFooterBytes.Length]; System.Buffer.BlockCopy(formHeaderBytes, 0, bodyBytes, 0, formHeaderBytes.Length); System.Buffer.BlockCopy(data, 0, bodyBytes, formHeaderBytes.Length, data.Length); System.Buffer.BlockCopy(formFooterBytes, 0, bodyBytes, formHeaderBytes.Length + data.Length, formFooterBytes.Length); request.uploadHandler = new UploadHandlerRaw(bodyBytes); request.downloadHandler = new DownloadHandlerBuffer(); yield return request.SendWebRequest(); if (request.result == UnityWebRequest.Result.Success) { Debug.Log("Upload complete!"); } else { Debug.Log("Error during upload: " + request.error); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值