1
votes

I'm trying to upload images from my mobile app to a web service build using VB.NET MVC 4 WEB API. The mobile app can upload successfully to a php api I tested it on, so I know the problem is my web service. I've search the web and have not found any working samples for uploading files, any suggestions would be appreciated.

Code:

Imports System.IO
Imports System.Net.Http
Public Class Pic

 Public Property pix As FileUpload

Public Function addPic() As String
    Try

         Dim savedFile As String
         savedFile = Path.GetFileName(Me.pix.PostedFile.FileName)
         Me.pix.PostedFile.SaveAs("C:\tmp" + savedFile)

    Catch exp As Exception
        'Response.Write(exp.Message)
    End Try

   End Function
End Class

Error:

No MediaTypeFormatter is available to read an object of type 'Pic' from content with media type 'multipart/form-data'.

ExceptionType:

System.InvalidOperationException

StackTrace:

at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable1 formatters, IFormatterLogger formatterLogger)\r\n at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable1 formatters, IFormatterLogger formatterLogger)\r\n at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable1 formatters, IFormatterLogger formatterLogger)\r\n at System.Web.Http.ModelBinding.FormatterParameterBinding.ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken)\r\n at System.Web.Http.Controllers.HttpActionBinding.<>c__DisplayClass1.<ExecuteBindingAsync>b__0(HttpParameterBinding parameterBinder)\r\n at System.Linq.Enumerable.WhereSelectArrayIterator2.MoveNext()\r\n at System.Threading.Tasks.TaskHelpers.IterateImpl(IEnumerator`1 enumerator, CancellationToken cancellationToken)"}

1

1 Answers

1
votes
Public Function Post()            
    Dim value1 As String = System.Web.HttpContext.Current.Request.Form("value1")

        Dim image1 As HttpPostedFile = System.Web.HttpContext.Current.Request.Files("value2")

        Dim filename As String = System.IO.Path.GetFileName(image1.FileName)

        pix.SaveAs("c:\tmp\" + filename)


    End Function