Advertisement
Guest User

EAP_API_Ass04

a guest
Jun 17th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.82 KB | None | 0 0
  1. [ValidateAntiForgeryToken]
  2.         [HttpPost]
  3.         public ActionResult Add(Mobile newM, HttpPostedFileBase image)
  4.         {
  5.             string filepath = "", filename = "", fileext = "", savepath = "";
  6.             if (ModelState.IsValid)
  7.             {
  8.                 if (image != null)
  9.                 {
  10.                     filepath = image.FileName;
  11.                     filename = newM.ID + "_" + Path.GetFileName(filepath);
  12.                     fileext = Path.GetExtension(filename);
  13.                     savepath = Server.MapPath("~/Images/" + filename);
  14.  
  15.                     if (CheckExt(fileext))
  16.                     {
  17.                         newM.Image = filename;
  18.                         //image.SaveAs(savepath);
  19.                     }
  20.                     else
  21.                     {
  22.                         ModelState.AddModelError("", "Only allow: png, jpg, jpeg, gif");
  23.                         return View();
  24.                     }
  25.                 }
  26.  
  27.                 var result = client.PostAsJsonAsync(baseURI, newM).Result;
  28.                 if (result.IsSuccessStatusCode)
  29.                 {
  30.                     if (image != null)
  31.                     {
  32.                         image.SaveAs(savepath);
  33.  
  34.                         // Upload through API
  35.                         var content = new MultipartFormDataContent();
  36.                         var fileContent = new StreamContent(image.InputStream);
  37.                         fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data")
  38.                         {
  39.                             Name = "UploadedFile",
  40.                             FileName = "\"" + filename + "\""
  41.                         };
  42.                         fileContent.Headers.ContentType = new MediaTypeHeaderValue(image.ContentType);
  43.                         content.Add(fileContent);
  44.                         client.PostAsync(baseURI + "upload", content);
  45.  
  46.                     }
  47.                     return RedirectToAction("Index");
  48.                 }
  49.  
  50.  
  51.                 ModelState.AddModelError("", result.StatusCode.ToString());
  52.  
  53.             }
  54.             return View();
  55.         }
  56.  
  57.  
  58. //-------------------------------------API----------------------------------
  59.  
  60. [HttpGet]
  61.         [Route("api/mobileapi/image/{id}")]
  62.         public IHttpActionResult image(int id)
  63.         {
  64.             string path = System.Web.Hosting.HostingEnvironment.MapPath("~/Images/" + GetMobile(id).Image);
  65.             var file = File.OpenRead(path);
  66.  
  67.             var response = new HttpResponseMessage(HttpStatusCode.OK);
  68.             response.Content = new StreamContent(file);
  69.  
  70.             //response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");
  71.             response.Content.Headers.ContentLength = file.Length;
  72.             return ResponseMessage(response);
  73.         }
  74.  
  75.         [Route("api/mobileapi/upload")]
  76.         [HttpPost]
  77.         public async Task<IHttpActionResult> upload()
  78.         {
  79.             var provider = new MultipartMemoryStreamProvider();
  80.             await Request.Content.ReadAsMultipartAsync(provider);
  81.             foreach (var file in provider.Contents)
  82.             {
  83.                 var filename = file.Headers.ContentDisposition.FileName.Trim('\"');
  84.                 var buffer = await file.ReadAsByteArrayAsync();
  85.                 var filePath = System.Web.Hosting.HostingEnvironment.MapPath("~/Images/" + filename);
  86.                 File.WriteAllBytes(filePath, buffer);
  87.             }
  88.  
  89.             return Ok();
  90.         }
  91.  
  92.  
  93.         [Route("api/mobileapi/delete/{id}")]
  94.         [HttpPost]
  95.         public IHttpActionResult delete(int id)
  96.         {
  97.             string path = System.Web.Hosting.HostingEnvironment.MapPath("~/Images/" + GetMobile(id).Image);
  98.             File.Delete(path);
  99.             return Ok();
  100.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement