只有1/4的图像会上传到webapi

时间:2016-07-14 00:24:41

标签: post image-processing asp.net-web-api xamarin

我目前正在尝试使用Xamarin表单从Android设备上传图像。 数据库的所有条目都正常工作,图像上传但只有图像的1/4左右

file that uploaded

我用相机拍照时,我一直使用相同的代码上传图片。

从手机图库中选择图像时会出现此问题。然后我通过在电话屏幕上显示它来检查整个选择是否正常。

图像开始上传,然后停止上传约3秒钟。我收到的错误消息是内部服务器错误"所以我将webapi上的web.config更新为。

<system.web.extensions>
<scripting>
  <webServices>
    <jsonSerialization maxJsonLength="2147483644"/>
  </webServices>
</scripting>
</system.web.extensions>

以及

maxRequestLength="50000"

但没有解决我的问题

这是我选择图片并上传的代码

 private async void SaveNewLogo(object sender, EventArgs e)
    {
        Stream stream = new MemoryStream(mysfile);
        var FixedFileName = DateTime.Now.Millisecond + "BusinessLogo_" + BusinessID + ".jpg";
        try
        {
            StreamContent scontent = new StreamContent(stream);
            scontent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
            {
                FileName = FixedFileName,
                Name = "image"
            };
            scontent.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");
            var client = new HttpClient();
            var multi = new MultipartFormDataContent();
            multi.Add(scontent);
            client.BaseAddress = new Uri("http://mywebapi.azurewebsites.net/");
            var result = client.PostAsync("api/Business/UpdateBusinessLogo/" + BusinessID, multi).Result;
            await DisplayAlert("Err", result.ToString(), "OK");

        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "An Error Occured at : SaveNewLogo MOBILE ");
            ExceptionUtility.NotifySystemOps(ex);
        }
    }

并从我的webapi收到数据

    <Route("api/Business/UpdateBusinessLogo/{businessId}")> _
   <HttpPost>
   <HttpGet>
    Function UpdateBusinessLogo(businessId As Integer)
        Try
            Dim root As String = HttpContext.Current.Server.MapPath("~/Files/BusinessLogos/Medium")
            Dim provider = New CustomMultipartFormDataStreamProvider(root)
            Dim task = Request.Content.ReadAsMultipartAsync(provider) _
            .ContinueWith(Of HttpResponseMessage)(Function(t)
                                                      If t.IsFaulted OrElse t.IsCanceled Then
                                                          Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception)
                                                      End If
                                                      For Each uploadedFile As MultipartFileData In provider.FileData
                                                      Next
                                                      Return Request.CreateResponse(HttpStatusCode.OK)
                                                  End Function)
            Dim path As String = provider.FileData(0).LocalFileName
            Dim pos As Integer = path.LastIndexOf("\") + 1
            Dim GeneratedFileName As String = path.Substring(pos, path.Length - pos)
            Dim GetBusiness = db.Businesses.SingleOrDefault(Function(x) x.ID = businessId)
            GetBusiness.BusinessLogoURL = "http://mywebapi.azurewebsites.net/Files/BusinessLogos/Medium/" & GeneratedFileName
            GetBusiness.BusinessLogoImageFile = GeneratedFileName
            db.SaveChanges()
            Return task
        Catch ex As Exception
            ExceptionUtility.LogException(ex, "An Error Occured at : UpdateBusinessLogo : ")
            ExceptionUtility.NotifySystemOps(ex)
            Return "False"
        End Try
    End Function

一切似乎都有效,数据库中的所有数据都可以保存。只是当我在文件系统上查看文件时的图像只有图像的一部分。

只是因为它可能有助于我包含我的Android调整大小代码。

  public byte[] ResizeTheImage(byte[] imageData, float width, float height)
    {
        // Load the bitmap
        Bitmap originalImage = BitmapFactory.DecodeByteArray(imageData, 0, imageData.Length);
        Bitmap resizedImage = Bitmap.CreateScaledBitmap(originalImage, (int)width, (int)height, false);

        using (MemoryStream ms = new MemoryStream())
        {
            resizedImage.Compress(Bitmap.CompressFormat.Jpeg, 100, ms);
            return ms.ToArray();
        }
    }

再次,如果我用相机拍照它工作正常但是从画廊中选择错误发生

修改

使用fidder发布到webapi时,我收到此错误消息

方法&#39; UpdateBusinessLogo&#39;在类型&#39; BusinessesController&#39;返回一个Task实例,即使它不是异步方法

1 个答案:

答案 0 :(得分:0)

现在修复了,所有需要的是使webapi函数成为异步函数.. opps !!