Ich verwende DropZone JS
mit MVC
. Die ActionMethod
, die die Bilder speichert, verwendet einen try catch. Nun, was würde ich von der ActionMethod
im Falle eines Fehlers zurückkehren müssen, so dass das Front-End das abholen und dem Benutzer die Fehlermarkierung anzeigen wird, anstatt zu zeigen, dass alles erfolgreich verlaufen ist.Was sollte ich von MVC-Controller im Falle eines Fehlers zurückgeben, um sicherzustellen, dass DropZone es abholen wird
Ist es mit DropZone
eingebaut oder muss ich es an Ereignis wie abgeschlossen binden? Wenn das so ist, wie?
Ein Beispiel für die DropZone JS
komplette Veranstaltung
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){
}
});
Wenn dies, ich habe gerade in MVC
funktionieren würde HttStatusCodeResult
wie return new HttpStatusCodeResult(HttpStatusCode.BadRequest)
und return new HttpStatusCodeResult(HttpStatusCode.Ok)
AKTUALISIERT zurückkehren konnte - 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);
}
Aus irgendeinem Grund, wenn etwas els e schlägt innerhalb dieser Methode fehl, DropZone
wird es nicht abholen, und es markiert, wie die Datei erfolgreich hochgeladen wurde. Ich will es einen Fehler zeigen, wenn etwas in der ActionMethod
Hören Sie auf das [Fehlerereignis] (http://www.dropzonejs.com/#event-error) 'dropzone.on ("Fehler", Funktion (Datei, Daten) {...}) '. Wo ist dein mvc Aktionscode? – Jasen
Jasen, füge deinen Kommentar als Antwort hinzu, damit ich ihn für dich markieren kann. Es funktioniert, aber ich musste auch das 'Erfolg'-Ereignis hinzufügen. – Sugafree