您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何接受文件POST

如何接受文件POST

看到http://www.asp.net/web-api/overview/formats-and-model-binding/html-forms- and-multipart- mime#multipartmime,尽管我认为这篇文章使它看起来比确实是。

基本上,

public Task<HttpResponseMessage> PostFile() 
{ 
    HttpRequestMessage request = this.Request; 
    if (!request.Content.IsMimeMultipartContent()) 
    { 
        throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); 
    }

    string root = Sy@R_419_2354@.Web.HttpContext.Current.Server.MapPath("~/App_Data/uploads"); 
    var provider = new MultipartFormDataStreamProvider(root);

    var task = request.Content.ReadAsMultipartAsync(provider). 
        ContinueWith<HttpResponseMessage>(o => 
    {

        string file1 = provider.BodyPartFileNames.First().Value;
        // this is the file name on the server where the file was saved

        return new HttpResponseMessage() 
        { 
            Content = new StringContent("File uploaded.") 
        }; 
    } 
    ); 
    return task; 
}
其他 2022/1/1 18:14:41 有362人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶