DropZone JS
とMVC
を使用しています。イメージを保存するActionMethod
は、try catchを使用しています。エラーが発生した場合はActionMethod
から戻る必要がありますので、フロントエンドがそのことを認識し、すべてが成功したことを示す代わりにエラーマークを表示します。MVCコントローラからエラーが発生した場合、DropZoneがそれを受け取るようにするにはどうすればいいですか
DropZone
で組み込まれていますか、それとも完全なイベントにバインドする必要がありますか?もしそうなら、どうですか?
DropZone JS
completeイベント
this.on("complete", function (file, response) {
// If an error has occurred, mark the item as failed
if (response.code != 200){
}
// If it went through successful, show that to the user
if (response.code == 200){
}
});
の例これがうまくいく場合は、MVC
に私はちょうどそのような return new HttpStatusCodeResult(HttpStatusCode.BadRequest)
やreturn new HttpStatusCodeResult(HttpStatusCode.Ok)
としてHttStatusCodeResult
UPDATEDを返すことができます - ActionMethod
[HttpPost]
public ActionResult SaveImages()
{
bool isSavedSuccessfully = true;
string fName = "";
try
{
foreach (string fileName in Request.Files)
{
HttpPostedFileBase file = Request.Files[fileName];
if (HttpPostedFileBaseExtensions.IsImage(file))
{
//Save file content goes here
fName = file.FileName;
if (file != null && file.ContentLength > 0)
{
var originalDirectory = new DirectoryInfo(string.Format("{0}Images\\", Server.MapPath(@"\")));
string pathString = Path.Combine(originalDirectory.ToString(), "Temp");
var fileName1 = Path.GetFileName(file.FileName);
bool isExists = Directory.Exists(pathString);
if (!isExists)
Directory.CreateDirectory(pathString);
var path = string.Format("{0}\\{1}", pathString, file.FileName);
file.SaveAs(path);
_testRepository.EditMainPicture("test", pathString, "imageText", 1);
}
}
}
}
catch (Exception ex)
{
// TODO Add error logging!!
isSavedSuccessfully = false;
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
return new HttpStatusCodeResult(HttpStatusCode.Ok);
}
何らかの理由で何かがels eがこの方法で失敗すると、DropZone
はファイルを取得せず、ファイルが正常にアップロードされたことを示します。私はそれが何がActionMethod
[エラーイベント(http://www.dropzonejs.com/#event-error) 'dropzone.on( "エラー"、関数(ファイル、データをリッスン){...}) '。あなたのMVCアクションコードはどこですか? – Jasen
Jasen、あなたのコメントを回答として追加してください。それはうまくいくが、私は 'success'イベントを追加しなければならなかった。 – Sugafree