前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >.net通过boundary上传文件

.net通过boundary上传文件

作者头像
SingYi
发布2022-07-13 20:30:27
发布2022-07-13 20:30:27
52700
代码可运行
举报
文章被收录于专栏:Lan小站Lan小站
运行总次数:0
代码可运行
代码语言:javascript
代码运行次数:0
复制
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版本的:

代码语言:javascript
代码运行次数:0
复制
    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()
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档