2012-07-18 85 views
0

我在asp.net mvc 3中使用了Valums uploader插件进行文件上传。以下是窗体字段和ajax查询上传按钮窗体中的视图。我不确定我是否做对了。我必须改变视图,以便当我选择文件上传表单字段的值时也发送。在asp.net mvc中使用Valums插件提交的Ajax表单提交3

浏览:

<link href="@Url.Content("~/Content/css/fileuploader.css")" rel="stylesheet" type="text/css" /> 
<script src="@Url.Content("~/Content/js/fileuploader.js")" type="text/javascript"></script> 


@using (Html.BeginForm("Upload","AjaxUpload")) { 
    @Html.ValidationSummary(true) 
    <fieldset> 
     <legend>Upload Image File</legend> 
     <div class="editor-label"> 
      @Html.Label("Select Language") 
     </div> 
     <div> 

      @Html.DropDownList("Language1", (SelectList) ViewBag.lang) 
     </div> 
     <div class="editor-label"> 
      @Html.Label("Select Category") 
     </div> 


     <div> 
      @Html.DropDownList("ParentCategoryID", ViewBag.ParentCategoryID as SelectList) 
     </div> 

     <div id="file-uploader"> 
    <noscript> 
     <p> 
      Please enable JavaScript to use file uploader.</p> 
    </noscript> 
</div> 
    </fieldset> 
} 


**<script type="text/javascript"> 
    var uploader = new qq.FileUploader 
    ({ 
     element: document.getElementById('file-uploader'), 
     action: '@Url.Action("upload")', // put here a path to your page to handle uploading 
     allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'], // user this if you want to upload only pictures 
     sizeLimit: 4000000, // max size, about 4MB 
     minSizeLimit: 0 // min size 
    }); 
</script>** 

我如何能够形成的价值传递给HTTPOST行动的控制器,以便我可以将数据保存到数据库中。在这里,我有上传行为,它将数据保存在数据库中,但我不知道要检索那些通过表单发送的值。

HttpPost行动

[HttpPost] 
     public ActionResult Upload(HttpPostedFileBase qqfile) 
     { 
      var wav = new PlayWav 
      { 
       Name = ***filename***, 
       CategoryID = ***value from category dropdown select list***, 
       UserID = repository.GetUserID(HttpContext.User.Identity.Name), 
       LanguageID = int.Parse(***value from language dropdown select list***), 
       UploadDateTime = DateTime.Now, 
       ActiveDateTime = DateTime.Now, 
       FilePath = "n/a" 
      }; 



      if (qqfile != null) 
      { 
       // this works for IE 
       var filename = Path.Combine(Server.MapPath("~/App_Data/Uploads"), Path.GetFileName(qqfile.FileName)); 
       qqfile.SaveAs(filename); 



       return Json(new { success = true }, "text/html"); 
      } 
      else 
      { 
       // this works for Firefox, Chrome 
       var filename = Request["qqfile"]; 
       if (!string.IsNullOrEmpty(filename)) 
       { 
        filename = Path.Combine(Server.MapPath("~/App_Data/Uploads"), Path.GetFileName(filename)); 
        using (var output = System.IO.File.Create(filename)) 
        { 
         Request.InputStream.CopyTo(output); 
        } 

        **db.PlayWavs.Attach(wav); 
        db.SaveChanges();** 

        return Json(new { success = true }); 
       } 
      } 
      return Json(new { success = false }); 
     } 

回答

1

你没看documentation?有一整段标题为发送附加参数。即使给出一个例子:

var uploader = new qq.FileUploader({ 
    element: document.getElementById('file-uploader'), 
    action: '/server-side.upload', 
    // additional data to send, name-value pairs 
    params: { 
     param1: 'value1', 
     param2: 'value2' 
    } 
}); 
+0

会PLZ告诉我,我怎样才能在控制器 – CodeManiac 2012-07-18 10:34:10

+1

的上传动作PARAMS值通过使用动作参数:'公众的ActionResult上传(HttpPostedFileBase qqfile,字符串参数1,字符串参数2)'甚至更好地使用视图模型:'公共ActionResult上传(MyViewModel模型)'其属性名称匹配您发送的参数。 – 2012-07-18 11:27:36