1

我正在尝试将图像从我的移动应用程序上传到使用 VB.NET MVC 4 WEB API 构建的 Web 服务。移动应用程序可以成功上传到我测试过的 php api,所以我知道问题出在我的网络服务上。我在网上搜索并没有找到任何用于上传文件的工作示例,任何建议将不胜感激。

代码:

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

错误:

没有 MediaTypeFormatter 可用于从媒体类型为“multipart/form-data”的内容中读取“Pic”类型的对象。

异常类型:

System.InvalidOperationException

堆栈跟踪:

在 System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent 内容,类型类型,IEnumerable 1 formatters, IFormatterLogger formatterLogger)\r\n at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable1 格式化程序,IFormatterLogger formatterLogger)\r\n 在 System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage 请求,类型类型, IEnumerable 1 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 cancelToken)"}

4

1 回答 1

1
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
于 2012-12-07T03:26:14.867 回答