如何在webapi中将MultipartFileStreamProvider类型转换为HttpPostedFileBase?

时间:2017-02-23 13:29:01

标签: c# asp.net-web-api asp.net-web-api2

我发送从Android客户端到web api的多部分请求它正在接收文件和值,但我需要将文件类型转换为HttpPostedFileBase我已经研究了很多,但他们都没有得到答案这是可能的让我发布我到目前为止所尝试的代码:

[HttpPost]
[Route("~/api/inoteupload")]
public async System.Threading.Tasks.Task<string> UploadFiles()
{
    int iUploadedCnt = 0;
    try
    {

        // DEFINE THE PATH WHERE WE WANT TO SAVE THE FILES.
        string sPath = @"D:\sfuploads";
        sPath = System.Web.Hosting.HostingEnvironment.MapPath("~/");
         MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider(sPath);
        MultipartFileStreamProvider multipartFileStreamProvider = await Request.Content.ReadAsMultipartAsync(streamProvider);
        String userid=streamProvider.FormData["userid"];
        String date = streamProvider.FormData["date"];
        String type = streamProvider.FormData["type"];
         HttpPostedFileBase hpf = null;
         UploadManager objUploadManager = new UploadManager(UploadTarget.INote, UploadFolder.Original);
        UploadFileInfo objUploadFileInfo = objUploadManager.Upload(hpf);
        iUploadedCnt = 1;
    }
    catch(Exception ex)
    {
        ex.ToString();
        iUploadedCnt = 0;
    }

    // RETURN A MESSAGE (OPTIONAL).
    if (iUploadedCnt > 0)
    {
        return iUploadedCnt + " Files Uploaded Successfully";
    }
    else {
        return "Upload Failed";
    }
}
在multipartFileStreamProvider中获取值和文件如何将该文件类型转换为httppostedfilebase。这可能是一个愚蠢的问题,但作为一个新的webapi和c#正在努力解决这个问题,有人可以帮助我吗?在此先感谢!!

0 个答案:

没有答案