开发了一个.net版本的服务端API示例,基于WebApi,async/task
Re开发了一个.net版本的服务端API示例,基于WebApi,async/task
楼主您好,我使用你的demo上传多媒体文件的时候总是报错,
internal byte[] UploadMedia(string url, string method, string fileName)
{
if (!File.Exists(fileName))
throw new FileNotFoundException('需要上传的文件不存在');
var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
fileStream.Seek(0, SeekOrigin.Begin);
var fileBuffer = new byte[fileStream.Length];
fileStream.Read(fileBuffer, 0, fileBuffer.Length);
fileStream.Dispose();
var boundary = GenerateRadomStr();
this.Headers['Content-Type'] = string.Format('multipart/form-data; boundary={0}', boundary);
string fileFormdataTemplate =
'\r\n--' + boundary +
'\r\nContent-Disposition:form-data;name=\'{0}\';filename=\'{1}\'' +
'\r\nContent-Type:application/octet-stream' +
'\r\n\r\n';
string formDataHeader = String.Format(fileFormdataTemplate, 'media', fileName);
byte[] formDataHeaderBuffer = Encoding.UTF8.GetBytes(formDataHeader);
string begin = $'--{boundary}\r\n';
byte[] beginBuffer = Encoding.UTF8.GetBytes(begin);
string end = $'\r\n--{boundary}--\r\n';
byte[] endBuffer = Encoding.UTF8.GetBytes(end); ;
byte[] dataStream = new byte[formDataHeaderBuffer.Length + beginBuffer.Length + fileBuffer.Length + endBuffer.Length];
formDataHeaderBuffer.CopyTo(dataStream, 0);
beginBuffer.CopyTo(dataStream, formDataHeaderBuffer.Length);
fileBuffer.CopyTo(dataStream, formDataHeaderBuffer.Length + begin.Length);
endBuffer.CopyTo(dataStream, formDataHeaderBuffer.Length + begin.Length + fileBuffer.Length);
//var returnBuffer = await this.UploadDataTaskAsync(url, 'POST', dataStream);
var returnBuffer = this.UploadData(url, 'POST', dataStream);
return returnBuffer;
}
提示我上传的多媒体类型错误
赞0
踩0