获取上传地址的接口 中,我们提供的接口支持PUT,文件实体将在 Body 传递,请问具体是放在body哪儿呢?

阅读次数 6

获取上传地址的接口 中,我们提供的接口支持PUT,文件实体将在 Body 传递,请问具体是放在body哪儿呢?是formdata、raw、binary?

2 Answers

使用application/octet-stream , 读取文件的二进制流

if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); }
var provider = new MultipartMemoryStreamProvider();
var task = Request.Content.ReadAsMultipartAsync(provider).ContinueWith(t =>
{
if (t.IsFaulted || t.IsCanceled)
{
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception));
}

foreach (var file in provider.Contents)
{
    string filename = file.Headers.ContentDisposition.FileName.Trim('\"');
    var buffer = file.ReadAsByteArrayAsync().Result;

    // 这里你可以将buffer保存为文件
    FileTools.FileUpload(buffer, filename, 2);
}

});

文件接收不到

非maltipart方式,试试读二进制流

文件名是否传了呢?

/// 
/// 文件上传
/// 
/// 
[HttpPut]
[Route("files/upload")]
public async Task UploadFile()
{
    RtnWpsResult rtn = new RtnWpsResult();
    string errexmsg = "", msg = "";
    try
    {
        await Task.Run(() =>
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }
            var provider = new MultipartMemoryStreamProvider();
            var task = Request.Content.ReadAsMultipartAsync(provider).ContinueWith(t =>
            {
                if (t.IsFaulted || t.IsCanceled)
                {
                    throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception));
                }

                foreach (var file in provider.Contents)
                {
                    string filename = file.Headers.ContentDisposition.FileName.Trim('\"');
                    var buffer = file.ReadAsByteArrayAsync().Result;

                    // 这里你可以将buffer保存为文件
                    FileTools.FileUpload(buffer, filename, 2);
                }
            });
        }, new CancellationToken());
    }
    catch (Exception ex)
    {
        errexmsg = ex.ToString();
        msg = ex.Message;
    }
    finally
    {
        servie.ErrLog("wps接口", $"WebOffice/v3/3rd/files/upload", "", errexmsg, msg);
    }
    return rtn;
}

我使用上面方法,在文件保存的时候,接口异常了呢