C# 文件上传在一个文件夹中
我想在BeginForm中添加文件上载功能,一旦我单击文件上载提交按钮,我想直接使用另一种方法上载该文件,而不是相同的BeginForm操作 如何使用Ajax函数实现这一点C# 文件上传在一个文件夹中,c#,jquery,ajax,asp.net-mvc,C#,Jquery,Ajax,Asp.net Mvc,我想在BeginForm中添加文件上载功能,一旦我单击文件上载提交按钮,我想直接使用另一种方法上载该文件,而不是相同的BeginForm操作 如何使用Ajax函数实现这一点 @using (Html.BeginForm("Add_Field_Values", "Home", new { Product_ID = ViewBag.Product_ID , FieldID = ViewBag.FieldID }, FormMethod.Post)) { @Html.AntiForgeryTo
@using (Html.BeginForm("Add_Field_Values", "Home", new { Product_ID = ViewBag.Product_ID , FieldID = ViewBag.FieldID }, FormMethod.Post))
{
@Html.AntiForgeryToken()
if (condition)
{
<input type="file" name="files">
<input id="adddetails" type="button" value="Upload File to Server">
}
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save Details" class="btn btn-success" />
</div>
}
<script>
$().ready(function () {
$("#adddetails").click(function () {
$.ajax({
type: "POST",
url: "/FileUpload/Financing_Product_Feature_Upload",
data:
{
....
},
success: function (data) {
}
});
});
</script>
我的问题是在上面定义可以上传文件的ajax
调用
请告知您可以这样做,但不幸的是,我认为您将无法将参数传递给控制器操作,如
productId
,但您可以做什么,您可以为该值设置一个隐藏字段
您的ajax表单应该如下所示:
$("#adddetails").click(function (evt) {
$.ajax({
type: "POST",
url: "/FileUpload/Financing_Product_Feature_Upload",
contentType: false,
processData: false,
data: new FormData($('.myForm').get(0)),
success: function (message) {
alert(message);
},
error: function () {
alert("There was error uploading files!");
}
});
});
@using (Html.BeginForm("Add_Field_Values", "Home", FormMethod.Post, new { @class = "myForm"})){
<input type="file" name="files" />
@Html.Hidden("ProductId", ViewBag.Product_ID);
}
[HttpPost]
public ActionResult Financing_Product_Feature_Upload()
{
var files = Request.Files;
var productId = Request.Params["ProductId"].ToString();
}
文件输入应如下所示:
$("#adddetails").click(function (evt) {
$.ajax({
type: "POST",
url: "/FileUpload/Financing_Product_Feature_Upload",
contentType: false,
processData: false,
data: new FormData($('.myForm').get(0)),
success: function (message) {
alert(message);
},
error: function () {
alert("There was error uploading files!");
}
});
});
@using (Html.BeginForm("Add_Field_Values", "Home", FormMethod.Post, new { @class = "myForm"})){
<input type="file" name="files" />
@Html.Hidden("ProductId", ViewBag.Product_ID);
}
[HttpPost]
public ActionResult Financing_Product_Feature_Upload()
{
var files = Request.Files;
var productId = Request.Params["ProductId"].ToString();
}
我希望这可能对你有用,你会得到我答案的主要概念,祝你好运;) 如果我有几个属性,而不是文件附件,那么如何在ajax中获取这些属性:new FormData($('.myForm').get(0)),?实际上,现在当
processData:false,
时,您不能将任何属性传递给纯控制器模型、文件或其他一些数据,我的建议是使用隐藏字段。不幸的是,我不知道任何其他解决方案如何在控制器操作时使用ajax检索文件或传递属性。实际上,我的意思是,如果我有多个属性而不是文件附件,那么如何获取准确的ID,您所说的$('.myForm')。get(0)
,这是否意味着第一个属性?Ohhh,它只是得到了表单,我想它的工作原理也是这样的newformdata($('.myForm'),
或者newformdata($('.myForm')).serialize()),
,因此,如果传递多个参数等等,它仍然会得到您需要的所有文件。