当前位置:网站首页 > 更多 > 涨姿势 > 正文

[每日一学] .net通过boundary上传文件

作者:CC下载站 日期:2021-10-16 00:00:00 浏览:55 分类:涨姿势

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()
您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯