.net通过boundary上传文件

Lan
Lan
2021-10-16 / 0 评论 / 256 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2021年10月16日,已超过1133天没有更新,若内容或图片失效,请留言反馈。

成功

public void UploadVedio(string fileName)
        {
            fileName = "input_video_only_3sec.mp4";
            byte[] vedioBytes = null;
            using (FileStream fileStream = new FileStream(@"D:\缓存内容\test.mp4", FileMode.Open, FileAccess.Read))
            {
                try
                {
                    vedioBytes = new byte[fileStream.Length];
                    fileStream.Read(vedioBytes, 0, (int)fileStream.Length);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            string cookie = "";
            string token = getUploadToken(cookie);
            string boundary = "------WebKitFormBoundary5TsAeTVHbPVlsrNh";
            string newline = "\r\n";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri(""));
            req.ContentType = $"multipart/form-data;boundary=----WebKitFormBoundary5TsAeTVHbPVlsrNh";
            req.Headers.Add("Authorization", token);
            byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n" + boundary + "\r\n");
            byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n" + boundary + "--\r\n");
            req.Method = "POST";
            Stream reqStream = req.GetRequestStream();
            string textTemplate = $"Content-Disposition: form-data; name=\"name\"" + newline;
            textTemplate += newline;
            textTemplate += fileName + newline;
            textTemplate += boundary + newline;
            textTemplate += $"Content-Disposition: form-data; name=\"size\"" + newline;
            textTemplate += newline;
            textTemplate += vedioBytes.Length + newline;
            textTemplate += boundary + newline;
            textTemplate += $"Content-Disposition: form-data; name=\"dir\"" + newline;
            textTemplate += newline + newline;
            textTemplate += boundary + newline;
            byte[] itemBytes = Encoding.UTF8.GetBytes(textTemplate);
            reqStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
            reqStream.Write(itemBytes, 0, itemBytes.Length);
            string fileTemplate = $"Content-Disposition: form-data; name=\"file\"; filename=\"{fileName}\"\r\nContent-Type: video/mp4" + newline + newline;
            byte[] fileBytes = Encoding.UTF8.GetBytes(fileTemplate);
            reqStream.Write(fileBytes, 0, fileBytes.Length);
            reqStream.Write(vedioBytes, 0, vedioBytes.Length);
            reqStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
            reqStream.Close();
            reqStream.Dispose();
            HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
            Encoding encoding = Encoding.UTF8;
            if (!string.IsNullOrEmpty(rsp.CharacterSet))
            {
                encoding = Encoding.GetEncoding(rsp.CharacterSet);
            }

        }

再来一个Python版本的:

    headers = {'Authorization': token, 'Content-Type': 'multipart/form-data;boundary=-------28947758029299'}
    url = ''
    fileContent = await file.read()
    multipart_encoder = MultipartEncoder(
        fields={'name': file.filename, 'size': str(len(fileContent)),
                'file': (file.filename, fileContent, 'video/mp4')}, boundary='-------28947758029299')
    headers['Content-Type'] = multipart_encoder.content_type
    return requests.post(url=url, headers=headers, data=multipart_encoder, verify=False).json()
1

评论 (0)

取消