private string uploadIcpMedia(HttpContext context)
        {
            string accessToken = context.Request.QueryString["access_token"];
            string type = context.Request.Form["type"];
            string certificateType = context.Request.Form["certificate_type"];
            string icpOrderField = context.Request.Form["icp_order_field"];
            HttpPostedFile file = context.Request.Files["media"];
            byte[] mediaByte= null;
            if (type== "video")
            {
                mediaByte = GetMediaBytes(file);
            }
            if (!string.IsNullOrEmpty(accessToken) && !string.IsNullOrEmpty(type) && !string.IsNullOrEmpty(icpOrderField))
            {

                if (file != null)
                {
                    string apiUrl = $"https://api.weixin.qq.com/wxa/icp/upload_icp_media?access_token={accessToken}";

                    var httpClient = new HttpClient();
                    string boundary = Guid.NewGuid().ToString("N");
                    var content = new MultipartFormDataContent(boundary);
                    content.Headers.ContentType = MediaTypeHeaderValue.Parse($"multipart/form-data;boundary=\"{boundary}\"");
                    content.Add(new StringContent(type), "type");
                    content.Add(new StringContent(certificateType), "certificate_type");
                    content.Add(new StringContent(icpOrderField), "icp_order_field");
                    file.InputStream.Seek(0, SeekOrigin.Begin);
                    if (type == "video")
                    {
                        content.Add(new ByteArrayContent(mediaByte), "media", $"\"{file.FileName}\"");
                    }
                    else
                    {
                        content.Add(new StreamContent(file.InputStream), "media", $"\"{file.FileName}\"");
                    }
                    
                    //string requestString = content.ReadAsStringAsync().Result;
                    var postResult = httpClient.PostAsync(apiUrl, content).Result;
                    string result = postResult.Content.ReadAsStringAsync().Result;

                    return result;
                }
                else
                {
                    return "No media data provided";
                }
            }
            else
            {
                return "Missing required parameters: access_token, type, and/or icp_order_field";
            }
        }

I think there should be an error in the content of 'content', but I don't know how to handle it. This is the request example provided in the interface documentation:

curl -F "type=image" -F "certificate_type=5" -F "icp_order_field=icp_applets.principal_info.certificate_photo_back" -F "[email protected]" "https://api.weixin.qq.com/wxa/icp/upload_icp_media?access_token=ACCESS_TOKEN"

This is the result of debugging "string requestString = content.ReadAsStringAsync().Result;" in Visual Studio. Because the request result is output as a string, the content of the file is displayed as garbled text.

--9ca34941f422418ca91d5e34adc98572
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=type

image
--9ca34941f422418ca91d5e34adc98572
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=certificate_type

5
--9ca34941f422418ca91d5e34adc98572
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=icp_order_field

icp_subject.principal_info.certificate_photo_front
--9ca34941f422418ca91d5e34adc98572
Content-Disposition: form-data; name=media; filename="bg.png"; filename*=utf-8''%22bg.png%22

�PNG

This is a successful case. It might be because there is only one parameter for file upload in the form-data. If you switch to the method mentioned earlier, it won't work when there are multiple parameters in the form-data.

private string uploadMedia(HttpContext context)
        {
            string accessToken = context.Request.QueryString["access_token"];
            string type = context.Request.QueryString["type"];
            HttpPostedFile file = context.Request.Files["media"];

            if (file != null && file.ContentLength > 0)
            {
                string apiurl = $"https://api.weixin.qq.com/cgi-bin/media/upload?access_token={accessToken}&type={type}";
                var httpClient = new HttpClient();
                string boundary = Guid.NewGuid().ToString("N");
                var content = new MultipartFormDataContent(boundary);
                content.Headers.ContentType = MediaTypeHeaderValue.Parse($"multipart/form-data;boundary={boundary}");
                file.InputStream.Seek(0, SeekOrigin.Begin);
                content.Add(new StreamContent(file.InputStream), "media", $"\"{file.FileName}\"");
                string requestString = content.ReadAsStringAsync().Result;
                var postResult = httpClient.PostAsync(apiurl, content).Result;
                string result = postResult.Content.ReadAsStringAsync().Result;

                return result;
            }
            else
            {
                return "No file uploaded";
            }
        }
0

There are 0 answers