Nicht in der Lage, upload-Datei per Ajax.BeginForm() asynchron

Ich versuche, eine Datei hochladen mittels Ajax.BeginForm(), aber es ist nicht der Arbeit aus.

Meiner Sicht enthält:

@using (Ajax.BeginForm("UploadFile", null, new AjaxOptions { HttpMethod="POST",     UpdateTargetId = "result" }, new { enctype = "multipart/form-data" }))
{
   <label id="lblUploadNewFile" for="fileUploadControl">Upload New File&lt;/label>
   <input type="file" name="fileToUpload" id="fileUploadControl"/>
   <input id="btnFileUpload" type="submit" value="Upload" />
   <span id="result" />
}

und den entsprechenden Controller ist:

[HttpPost]
public string UploadFile(FormCollection formData)
{
   HttpPostedFileBase file=null;

   try
   {
      file = Request.Files[0];
   }
   catch { }

   if ( file!=null &amp;&amp; file.ContentLength &gt; 0)
   {
      file.SaveAs(string.Concat(
            AppDomain.CurrentDomain.BaseDirectory,
            Path.GetFileName(file.FileName)));

      return &quot;Successfully Uploaded&quot;;
   }
   else
   {
      return &quot;Upload Failed, please try again.&quot;;
   }
}

Das problem ist, dass es die Datei hochladen, aber nicht mehr tun, jede asynchrone Beiträge, wenn ich Sie entfernen jquery.unobtrusive-ajax.js. Stattdessen, es bedeutet eine vollständige post-back.

Wenn ich jquery.unobtrusive-ajax.js in meinem view, Sie tut es asynchron, aber es ist nicht das senden einer Datei hochladen, die in form von Daten. Keine Datei wird an den server gesendet Request.Files[].

  • Es ist nicht möglich, mit Ajax aus offensichtlichen Gründen: Sie bauen konnte Seite hat-uploads jede zugängliche Datei auf die Besucher Dateisystem auf den server-ohne ihn.
InformationsquelleAutor Sunny Sharma | 2013-06-11
Schreibe einen Kommentar