一个简单的C#后端+ajax上传文件的demo
前端部分
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script type="text/javascript" src="../jquery.min.js"></script>
</head>
<body>
<input type="file" name="sss" id="theFile">
<script>
$(function () {
$('#theFile').change(function () {
debugger;
var crowd_file = $('#theFile')[0].files[0];
var formData = new FormData();
formData.append("file", crowd_file);
$.ajax({
url: 'https://localhost:44370/home/upload',
async: false,
type: 'post',
data: formData,
processData: false, // 使数据不做处理
contentType: false, // 不要设置Content-Type请求头
success: function (data) {
debugger;
},
error: function (response) {
debugger;
}
});
});
});
</script>
</body>
</html>
C#部分
public ActionResult Upload()
{
var fileName = Request.Files.Get("file").FileName;
return null;
}