Web API - 如何使用Fiddler上传带有请求参数的excel文件

时间:2015-04-12 06:57:45

标签: asp.net-web-api fiddler

我通过Web Api在服务器上上传excel文件。我需要传递一些参数和文件。

但是当我使用fiddler时,我可以获得Request参数或仅提供文件,但我需要在我的Web API控制器中使用这两种方法。

方法类型:发布 网址:http://localhost/MP.Services/api/catalog/file/upload/

请求标题 -

User-Agent:Fiddler 主持人:localhost 内容长度:74 Content-Type:application / json;字符集= UTF

申请机构 - { " CatalogCode":" 1&#34 ;, "动作":" 1&#34 ;, "实体":" 1&#34 ;, " UploadedBy":" 1" }

上面是我传递请求参数的正常方式,我尝试从上传文件'上传fiddler中的excel文件。选项以及上述请求,但当文件上传请求标头和请求正文得到更改。 当我运行它时,我将无法将请求参数提供给我的Web API控制器。

我的控制器操作代码 -

[HttpPost]
        public async Task<HttpResponseMessage> UploadCatalogExcel(CatalogUploadRequest catalogUploadRequest)
        {
            if (catalogUploadRequest == null)
                return CreateResponse(HttpStatusCode.NotAcceptable, ControllerErrorCodeConstants.RequestIsInvalid, "Invalid request");

            if (!ModelState.IsValid) return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);

            if (!Request.Content.IsMimeMultipartContent())
                return Request.CreateResponse(HttpStatusCode.UnsupportedMediaType, "Uploading file is mandatory");

            try
            {
                //Request to BL Mapping

                var catalogUploadExcelBi = CatalogUploadMapping.UploadCatalogExcelRequestToBiMap(catalogUploadRequest);

                var CatalogInfo = _catalogUploadBi.GetSampleCatalogExcel(catalogUploadExcelBi.CatalogCode);

                string uploadPath = HttpContext.Current.Server.MapPath("~/App_Data");
                uploadPath = uploadPath + "\\Upload\\" + CatalogInfo.SellerAccountId + "_" + CatalogInfo.SellerAccountName + "\\" + DateTime.Now.Year.ToString() + "_" + CatalogInfo.CatalogCode; // Physical File Location

                string currentTime = Regex.Replace(DateTime.Now.ToString(), "[^0-9]+", "");
                string name = catalogUploadExcelBi.CatalogCode + "_" + currentTime + ".xlsx"; // File Name                              

                catalogUploadExcelBi.FileName = "aa";
                catalogUploadExcelBi.FilePath = uploadPath;                            

                bool exists = System.IO.Directory.Exists(uploadPath);
                if (!exists)
                    System.IO.Directory.CreateDirectory(uploadPath);

                MyStreamProvider streamProvider = new MyStreamProvider(uploadPath);
                await Request.Content.ReadAsMultipartAsync(streamProvider);   

                var response = _catalogUploadBi.LogUploadCatalogExcel(catalogUploadExcelBi);

                if (response.ServerErrors != null && response.ServerErrors.Count != 0)
                {
                    response.ServerErrors = response.ServerErrors;
                    return Request.CreateResponse(HttpStatusCode.BadRequest, response.ServerErrors);
                }
                return Request.CreateResponse(HttpStatusCode.OK, "File uploaded successfully");
            }
            catch (Exception ex)
            {
                var error = _errorManager.GetCustomeError(ex.GetType().ToString());

                return error != null && !string.IsNullOrWhiteSpace(error.ErrorCode) && !string.IsNullOrWhiteSpace(error.ErrorMessage)
                    ? Request.CreateResponse(string.Format("Exception Occured! Error code : {0}  Error Message :   {1}", error.ErrorCode,
                        error.ErrorMessage)) : Request.CreateResponse(string.Format("Upload file method Exception Occured!"));
            }           
        }

0 个答案:

没有答案