1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
Imports System Imports System.Collections.Generic Imports System.Linq Imports System.Net Imports System.Net.Http Imports System.Threading.Tasks Imports System.Web Imports System.Web.Http Namespace CrystalReportIn_Webapi.Controllers <RoutePrefix("api/Upload")> Public Class UploadController Inherits ApiController <Route("user/PostUserImage")> <AllowAnonymous> Public Async Function PostUserImage() As Task(Of HttpResponseMessage) Dim dict As Dictionary(Of String, Object) = New Dictionary(Of String, Object)() Try Dim httpRequest = HttpContext.Current.Request For Each file As String In httpRequest.Files Dim response As HttpResponseMessage = Request.CreateResponse(HttpStatusCode.Created) Dim postedFile = httpRequest.Files(file) If postedFile IsNot Nothing AndAlso postedFile.ContentLength > 0 Then Dim MaxContentLength As Integer = 1024 * 1024 * 1 Dim AllowedFileExtensions As IList(Of String) = New List(Of String) From { ".jpg", ".gif", ".png" } Dim ext = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf("."c)) Dim extension = ext.ToLower() If Not AllowedFileExtensions.Contains(extension) Then Dim message = String.Format("Please Upload image of type .jpg,.gif,.png.") dict.Add("error", message) Return Request.CreateResponse(HttpStatusCode.BadRequest, dict) ElseIf postedFile.ContentLength > MaxContentLength Then Dim message = String.Format("Please Upload a file upto 1 mb.") dict.Add("error", message) Return Request.CreateResponse(HttpStatusCode.BadRequest, dict) Else Dim filePath = HttpContext.Current.Server.MapPath("~/Userimage/" & postedFile.FileName & extension) postedFile.SaveAs(filePath) End If End If Dim message1 = String.Format("Image Updated Successfully.") Return Request.CreateErrorResponse(HttpStatusCode.Created, message1) Next Dim res = String.Format("Please Upload a image.") dict.Add("error", res) Return Request.CreateResponse(HttpStatusCode.NotFound, dict) Catch ex As Exception Dim res = String.Format("some Message") dict.Add("error", res) Return Request.CreateResponse(HttpStatusCode.NotFound, dict) End Try End Function End Class End Namespace |