如何使用 ajax 保存文件
How to save file using ajax
我正在尝试通过 ajax 向控制器发送 FormData
来保存文件,问题是即使我使用 FormData
函数来我的 ajax 也没有工作将所有变量转化为一种形式。
我尝试过使用表单,但是在table中,我无法使用表单标签,因为我的按钮在另一个td 中。当我将它用于整个 table 时,它也不起作用,我创建了 FormData
并在其中附加了所有变量,其余变量都在起作用,但图像不起作用。
index.cshtml:
<tr class="form" style="display:none">
<td class="name">
<input type="text" name="Jméno" value="" />
</td>
<td class="price">
<input type="number" name="Cena" value="" />
</td>
<td class="quantity">
<input type="number" name="Mnozstvi" value="" />
</td>
<td class="image">
<input type="file" id="fileOne" name="fileOne" runat="server" />
</td>
<td>
<button class="FirstSave" type="button" id="btnUpload">Uložit</button>
<button class="getBack">Zahodit</button>
</td>
</tr>
这是我的 JavaScript
$(".FirstSave").click(function () {
let file = document.getElementById('fileOne').file;
let form = new FormData;
form.append('Image', file);
form.append('Name', name);
form.append('Price', price);
form.append('Quantity', quantity);
$.ajax({
url: "Insert",
method: "POST",
cache: false,
processData: false,
data: form,
});
})
产品 getter 和 setter
public class Product
{
public string Name { get; set; }
public int Price { get; set; }
public int Quantity { get; set; }
public string ImageUrl { get; set; }
public HttpPostedFileBase Image { get; set; }
}
产品控制器
[HttpPost]
public ActionResult Insert(Product product)
{
HttpPostedFileBase file = product.Image;
if (file != null)
{
var fileName = Path.GetFileName(file.FileName);
product.ImageUrl = Path.Combine(Server.MapPath("~/Content/Images/Products/"), fileName);
file.SaveAs(product.ImageUrl);
}
return null;
}
这是适合我的代码:
$(document).on("submit", "#myFormId", function (event) {
event.preventDefault();
event.stopImmediatePropagation();
var formData = new FormData(this);
$.ajax({
url: 'my url',
type: 'POST',
data: formData,
success: function (response) {
if (response) {
// Do whatever you want to do with response
}
},
error: function (error) {
console.log(error)
},
cache: false,
contentType: false,
processData: false
});
return false;
});
我发现您的 ajax 设置中缺少 contentType: false
。这是使用 ajax.
上传文件所必需的
改用这个:
var fileName = file.FileName;
更换你的线路
var fileName = Path.GetFileName(file.FileName);
所以如果我要更改您的代码:
var fileName = file.FileName;
product.ImageUrl = Path.Combine(Server.MapPath("~/App_Data/"), fileName);
file.SaveAs(product.ImageUrl);
如果这还不够,请检查我用于其中一个上传的 js 代码:
for (var x = 0; x < files.length; x++) {
data.append("file" + x, files[x]);
}
首先确认你的表单标签包含表单
enctype="multipart/form-data"。
第二个添加 contentType: false 在你的 ajax 调用
$.ajax({
url: form_url,
type:form_method,
内容类型:假,
数据:formData,});
我正在尝试通过 ajax 向控制器发送 FormData
来保存文件,问题是即使我使用 FormData
函数来我的 ajax 也没有工作将所有变量转化为一种形式。
我尝试过使用表单,但是在table中,我无法使用表单标签,因为我的按钮在另一个td 中。当我将它用于整个 table 时,它也不起作用,我创建了 FormData
并在其中附加了所有变量,其余变量都在起作用,但图像不起作用。
index.cshtml:
<tr class="form" style="display:none">
<td class="name">
<input type="text" name="Jméno" value="" />
</td>
<td class="price">
<input type="number" name="Cena" value="" />
</td>
<td class="quantity">
<input type="number" name="Mnozstvi" value="" />
</td>
<td class="image">
<input type="file" id="fileOne" name="fileOne" runat="server" />
</td>
<td>
<button class="FirstSave" type="button" id="btnUpload">Uložit</button>
<button class="getBack">Zahodit</button>
</td>
</tr>
这是我的 JavaScript
$(".FirstSave").click(function () {
let file = document.getElementById('fileOne').file;
let form = new FormData;
form.append('Image', file);
form.append('Name', name);
form.append('Price', price);
form.append('Quantity', quantity);
$.ajax({
url: "Insert",
method: "POST",
cache: false,
processData: false,
data: form,
});
})
产品 getter 和 setter
public class Product
{
public string Name { get; set; }
public int Price { get; set; }
public int Quantity { get; set; }
public string ImageUrl { get; set; }
public HttpPostedFileBase Image { get; set; }
}
产品控制器
[HttpPost]
public ActionResult Insert(Product product)
{
HttpPostedFileBase file = product.Image;
if (file != null)
{
var fileName = Path.GetFileName(file.FileName);
product.ImageUrl = Path.Combine(Server.MapPath("~/Content/Images/Products/"), fileName);
file.SaveAs(product.ImageUrl);
}
return null;
}
这是适合我的代码:
$(document).on("submit", "#myFormId", function (event) {
event.preventDefault();
event.stopImmediatePropagation();
var formData = new FormData(this);
$.ajax({
url: 'my url',
type: 'POST',
data: formData,
success: function (response) {
if (response) {
// Do whatever you want to do with response
}
},
error: function (error) {
console.log(error)
},
cache: false,
contentType: false,
processData: false
});
return false;
});
我发现您的 ajax 设置中缺少 contentType: false
。这是使用 ajax.
改用这个:
var fileName = file.FileName;
更换你的线路
var fileName = Path.GetFileName(file.FileName);
所以如果我要更改您的代码:
var fileName = file.FileName;
product.ImageUrl = Path.Combine(Server.MapPath("~/App_Data/"), fileName);
file.SaveAs(product.ImageUrl);
如果这还不够,请检查我用于其中一个上传的 js 代码:
for (var x = 0; x < files.length; x++) {
data.append("file" + x, files[x]);
}
首先确认你的表单标签包含表单 enctype="multipart/form-data"。 第二个添加 contentType: false 在你的 ajax 调用
$.ajax({ url: form_url, type:form_method, 内容类型:假, 数据:formData,});