当前位置:网站首页>C wechat upload form data

C wechat upload form data

2022-06-11 07:40:00 Qianjinshao

Wechat applet audit sometimes requires image upload verification Using or from-data Format , Very pitiful

The official documents are not clear at all

Request address :

https://api.weixin.qq.com/wxa/uploadmedia?access_token=ACCESS_TOKEN

Here are some places where you can upload , I often report errmsg=media data missing rid: 629f1365-2b583ac9-3f11b616, After an afternoon's test, I finally found out the reason .httpClient Will automatically boundary Auto double quotes , Cause the call to fail . So it needs to be handled manually
 

 

Attached directly at the end C# Uploaded code

        public async Task<string> AccessFormData(string url, HttpPostedFileBase file, string key)
        {
            var httpClient = new HttpClient();
            var content = new MultipartFormDataContent();
            var boundaryValue = content.Headers.ContentType.Parameters.Single(p => p.Name == "boundary");
            boundaryValue.Value = boundaryValue.Value.Replace("\"", string.Empty);
            if (file?.InputStream != null)
            {
                content.Add(new StreamContent(file.InputStream, (int) file.InputStream.Length), $"\"{key}\"",
                    $"\"{file.FileName}\"");
                var response = await httpClient.PostAsync(url, content);
                return await response.Content.ReadAsStringAsync();
            }
            return string.Empty;
        }

there key That's it. media

原网站

版权声明
本文为[Qianjinshao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110738588787.html