2016-08-06 11 views
1

Ich bin neu in mvc4. Ich benutzen die folgenden Code-Datei vom Server zum Client in der Steuerung zum Download:Datei-Download funktioniert nicht in asp.net mvc 4

public ActionResult IndexSpecification(int option_id) 
{ 
    int cat = (int)Session["category_Name"]; 
    int prod = (int)Session["product_ID"]; 
    int user = (int)Session["logged_in"]; 
    string twoinone = cat + "_" + prod; 
    f1 = Download(twoinone); 
    return f1; 
} 

wo Download-Funktion ist:

public FileResult Download(string twoinone) 
{ 
    var webClient = new WebClient(); 
    byte[] fileBytes = System.IO.File.ReadAllBytes(Path.Combine(Server.MapPath("~/Download"), "a.rar")); 
    string fileName = "Tellersoft.rar"; 
    return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName); 
} 

Der Aufruf an die Steuerung ist von Ajax:

$.ajax({ 
    type: "POST", 
    url: base_url + '/Options/IndexSpecification', 
    data: { option_id : 2 }, 
    //dataType: 'json', encode: true, 
    async: false, 
    cache: false, 
    success: function (data, status, jqXHR) {  
     console.log(data); 
    }, 
    error: function (jqXHR, textStatus, errorThrown) { 
     if (typeof (console) != 'undefined') { 
      alert("oooppss"); 
     } else { 
      alert("something went wrong"); 
     } 
    } 
}); 

Aber das Herunterladen funktioniert nicht. Es wird nicht einmal ein Fehler zurückgegeben. Bitte helfen

Antwort

0
$.ajax({ 
    type: "POST", 
    url: base_url + '/Options/IndexSpecification', 
    data: { option_id : 2 }, 
    //dataType: 'json', encode: true, 
    async: false, 
    cache: false, 
    success: function (data, status, jqXHR) { 
     console.log(data); 
    }, 
    error: function (jqXHR, textStatus, errorThrown) { 
     if (typeof (console) != 'undefined') { 
      alert("oooppss"); 
     } else { 
      alert("something went wrong"); 
     } 
    } 
}); 

In Ihrem Ajax-Request in Erfolg Block müssen Sie diesen Code schreiben:

$.ajax({ 
    type: "POST", 
    url:'/Home/Download', 
    data: { option_id: 2 }, 
    //dataType: 'json', encode: true, 
    async: false, 
    cache: false, 
    success: function (data, status, jqXHR) { 
     window.location = '/Home/Download?option_id=2'; 
    }, 
    error: function (jqXHR, textStatus, errorThrown) { 
     if (typeof (console) != 'undefined') { 
      alert("oooppss"); 
     } else { 
      alert("something went wrong"); 
     } 
    } 
}); 

Sie Ihre Option ID-Wert global gesetzt haben und erklären es in Erfolg Abschnitt ...

enter image description here

+0

Bitte versuchen Sie, Ihren Code richtig zu formatieren ... – andreas