2017-04-06 42 views
0

我試圖使用asp.net webapi將圖像保存到數據庫。在此控制器中,將圖像保存到我的/Content/Banner/文件夾中。在asp.net web api中將值傳遞給數據庫

[HttpPost] 
     [Route("PostBanner")] 
     [AllowAnonymous] 
     public HttpResponseMessage PostBannerImage() 
     { 
      Dictionary<string, object> dict = new Dictionary<string, object>(); 
      try 
      { 
      var httpRequest = HttpContext.Current.Request; 

      foreach (string file in httpRequest.Files) 
      { 
       HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created); 

       var postedFile = httpRequest.Files[file]; 
       if (postedFile != null && postedFile.ContentLength > 0) 
       { 

        int MaxContentLength = 1024 * 1024 * 1; //Size = 1 MB 

        IList<string> AllowedFileExtensions = new List<string> { ".jpg", ".gif", ".png" }; 
        var ext = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf('.')); 
        var extension = ext.ToLower(); 
        if (!AllowedFileExtensions.Contains(extension)) 
        { 

         var message = string.Format("Please Upload image of type .jpg,.gif,.png."); 

         dict.Add("error", message); 
         return Request.CreateResponse(HttpStatusCode.BadRequest, dict); 
        } 
        else if (postedFile.ContentLength > MaxContentLength) 
        { 

         var message = string.Format("Please Upload a file upto 1 mb."); 

         dict.Add("error", message); 
         return Request.CreateResponse(HttpStatusCode.BadRequest, dict); 
        } 
        else 
        { 
         var filePath = HttpContext.Current.Server.MapPath("~/Content/Banner/" + postedFile.FileName + extension); 
         postedFile.SaveAs(filePath); 
        } 
       } 

       var message1 = string.Format("Image Updated Successfully."); 
       return Request.CreateErrorResponse(HttpStatusCode.Created, message1); ; 
      } 

      var res = string.Format("Please Upload a image."); 
      dict.Add("error", res); 
      return Request.CreateResponse(HttpStatusCode.NotFound, dict); 
     } 
     catch (Exception ex) 
     { 
      var res = string.Format("some Message"); 
      dict.Add("error", res); 
      return Request.CreateResponse(HttpStatusCode.NotFound, dict); 
     } 
    } 

現在我需要發送文件路徑數據庫。我知道我只需要將此文件路徑傳遞給以下服務控制器。但我不知道如何通過它。任何人都可以幫助我解決這個問題。

這是我services.cs

public async Task<int?> Addbanner(DisplayBannersDto dto) 
     { 
      try 
      { 
       var d = _dbContext.Banners 
      .FirstOrDefault(); 



       d.Banner_Description = dto.Description; 
       d.Banner_Location = dto.Location; 


       //mark entry as modifed 
       _dbContext.Entry(d).State = EntityState.Modified; 
       await _dbContext.SaveChangesAsync(); 
       return d.Banner_Id; 
      } 
      catch (Exception ex) 
      { 
       throw ex; 
      } 
     } 

這是我的控制器

[HttpPost] 
     [Route("AddBanner")] 
     public async Task<IHttpActionResult> AddBanner(DisplayBannersDto dto) 
     { 
      if (!ModelState.IsValid) 
       return BadRequest(ModelState); 

      int? result = await _service.Addbanner(dto); 
      return Ok(); 

     } 
+0

是否要從PostBannerImage()API內部保存圖像路徑,還是要調用第二個API來執行數據庫保存? –

+0

@JoseFrancis我想調用第二個API來執行數據庫保存.. – ManojKanth

+0

那麼,爲什麼不從PostBannerImage()返回「文件路徑」,在客戶端接收,然後在插入相同內容後調用第二個API DisplayBannersDto。 –

回答

1

更改else條件如下圖所示:

else 
{ 
    var filePath = HttpContext.Current.Server.MapPath("~/Content/Banner/" + postedFile.FileName + extension); 
         postedFile.SaveAs(filePath); 

    return new HttpResponseMessage() 
      { 
       StatusCode = HttpStatusCode.Created, 
       Content = new StringContent(filePath, Encoding.UTF8, "application/json") 
      }; 
} 

現在,你有文件路徑中API響應的內容。

,並使用類似的迴應:

var displayBannersDto = new DisplayBannersDto(); 
displayBannersDto.Banner_Location = response.Content.ToString(); 

然後調用與displayBannersDto您AddBanner()API作爲DTO。

相關問題