現在、Xamarinフォームを使用してAndroidデバイスから画像をアップロードしようとしています。 私が写真を撮るとき、私は、画像をアップロードするには、同じコードを使用してきたすべてのデータベース作業の罰金にエントリーして画像をアップロードんが、唯一の約1/4の画像の画像の1/4のみがwebapiにアップロードされます
カメラ。
この問題は、電話ギャラリーから画像を選択したときに発生します。私はそれから全体が選択されていることを確認しました。
イメージのアップロードが開始され、約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
すべてが機能しているように見えますが、dbのすべてのデータが節約されます。ちょうど私がファイルシステム上のファイルを見ると、画像の一部しかありません。
ちょうど私はアンドロイドのための私のサイズ変更コードが含まれてilを助けるかもしれないそれを入れてください。再び
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に投稿するときにエラーが
EDIT
をたまたま私はこのエラーメッセージ
を取得'BusinessesController'タイプの 'UpdateBusinessLogo'メソッドは、非同期メソッドではないにもかかわらずTaskインスタンスを返しました