如何在ASP.Net Core MVC中上传图片保存在指定文件夹中前端的js代码怎么写
时间: 2024-03-20 16:41:48 浏览: 120
在ASP.Net Core MVC中,可以使用以下的前端js代码实现上传图片并保存在指定文件夹中:
```
<script>
$(document).ready(function () {
$('#btnUpload').click(function () {
var fileUpload = $('#image').get(0);
var files = fileUpload.files;
var formData = new FormData();
for (var i = 0; i < files.length; i++) {
formData.append(files[i].name, files[i]);
}
$.ajax({
type: "POST",
url: "/Home/Upload",
data: formData,
contentType: false,
processData: false,
success: function (response) {
alert("图片上传成功!");
},
error: function (error) {
alert("图片上传失败!");
}
});
});
});
</script>
```
在上面的代码中,使用jQuery的ajax方法向控制器的上传方法提交表单数据。其中,formData对象包含上传的图片文件,contentType设置为false表示让浏览器自动为请求添加正确的Content-Type头,processData设置为false表示不对数据进行序列化处理。可以根据需要修改上传路径和相关参数。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)