2013-05-15 27 views

回答

0

如果我理解正确,你想有设置在模型中值的输入文件控件的值。

由于安全原因,您不能这样做。否则,您可以将控件设置为“c:\ passwords.txt”或其他东西,并在用户不知道您已经这样做的情况下使用一点javascript来上传文件。

0

是的,你可以用一个jQuery和HTML 5兼容的浏览器来做到这一点。

$('#YourFormId').submit(function() { 
    var formData = new FormData($('form')[0]); 
    $.ajax({ 
     url: this.action, 
     cache: false, 
     type: this.method, 
     data: formData, 
     success: function (result) { 

     }, 
     beforeSend: function (e) { 

     }, 
     contentType: false, 
     processData: false 
    }); 
    // it is important to return false in order to 
    // cancel the default submission of the form 
    // and perform the AJAX call 
    return false; 
}); 

我从一些地方得到了这个代码,可能是这个网站。我不断看到人们说你不能这样做,但它适用于IE 10+和Chrome。

视图模型看起来是这样的:

public class MyModel 
{ 
    public string mytest { get; set; } 
    public string mytest2 { get; set; } 
    public string status { get; set; } 
    public HttpPostedFileBase fileupload {get;set;} 
} 

行动看起来是这样的:

[HttpPost] 
    public ActionResult YourActionName(YourModel model) 
    { 
     return whatever(); 
    } 
相关问题