【发布时间】:2016-07-14 09:04:47
【问题描述】:
我目前正在尝试使用 Xamarin 表单从 android 设备上传图像。 数据库中的所有条目都可以正常工作,并且图像确实上传了,但只有图像的 1/4 左右
当我用相机拍照时,我一直在使用相同的代码上传图像。
当我从手机图库中选择图片时会出现此问题。然后我通过在手机屏幕上显示它来检查整个是否被选中。
图像开始上传,然后大约 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 时,我收到此错误消息
“BusinessesController”类型的“UpdateBusinessLogo”方法返回了一个 Task 实例,即使它不是异步方法
【问题讨论】:
标签: post image-processing asp.net-web-api xamarin