jQuery File Uploadプラグインを使用して Web サービスを呼び出し、画像をサーバーにアップロードしています。
画像は正常にアップロードされ、画像が 4MB を超える場合に例外をスローする検証を Web サービスに追加しました。
ファイルアップロードプラグインの使用方法は次のとおりです。
var fileUploadInput = $('#uploader').fileupload({
dataType: 'json',
add: function (e, data) {
mainObject._addFile(e, data, mainObject);
},
done: function (e, data) {
mainObject._fileUploadDone(e, data, mainObject);
},
fail: function (e, data) {
mainObject._fileUploadFail(e, data, mainObject)
}
});
現在、ファイルのアップロードが失敗すると、パラメーターには次のようdata
なオブジェクトが含まjqXHR
れます。
readyState: 4
status: 200
statusText: "success"
Chrome では、jqXHR には、Web サービスによってスローされた例外によって生成された素敵なエラー メッセージが含まれています。Fiddler では、応答が 400 Bad Response であることがわかり、応答本文にエラー メッセージが表示されます。
これがIE8で取り上げられない理由を知っている人はいますか?
アップデート
サーバー側コードの簡略版:
[OperationContract(), WebInvoke(UriTemplate = "/Images", Method = "POST")]
public System.Guid CreateImage(System.IO.Stream Image)
{
//IE8 needs response to be in plain text format, or it will attempt to download the response :(
WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-Type", "text/plain");
//perform validation
BLL.Errors validationErrors = BLL.Utilities.ImageFileProcessing.ValidateSizeAndFormat(multipartRequestParser.FileContents);
//if there were errors
if (validationErrors.Count > 0)
{
//send back the errors
throw new WebFaultException<BLL.Errors>(validationErrors, System.Net.HttpStatusCode.BadRequest);
}
//save the image (returns the identifier for the image)
System.Guid imageId = BLL.Custom.BeamUSBuyingWindow.DataTransferObjects.Brand.SaveImage(multipartRequestParser.FileContents);
//set the response status and URI for created resource
WebOperationContext.Current.OutgoingResponse.SetStatusAsCreated(new System.Uri(WebOperationContext.Current.IncomingRequest.UriTemplateMatch.BaseUri.AbsoluteUri + "/" + imageId.ToString()));
return imageId;
}
このメソッドは、期待しているエラー テキストを含むオブジェクトValidateSizeAndFormat
の配列を返します。Errors
かなり大きなアプリです。
Chrome が jqXHR オブジェクトで 400 エラーを検出し、IE8 が 200 成功を検出していることに少し困惑しています。他の場所でも同様の Ajax エラー処理が行われますが、この問題はファイル アップロード プラグインでのみ発生します。