本文粗略的讲下前端文件上传和后端文件接收的原理
- 前端代码
html
<form onsubmit="uploadFile(event)"> <input type="file" /> <button type="submit">文件上传</button> </form>
js
function uploadFile(event) { var files = event.target[0].files; if (files.length < 1) { alert("文件没有选择,亲"); return; } var formData = new FormData(); for (var i = 0; i < files.length; i++) { formData.append(files[i].name, files[i]); } axios.post("yejiawei/haha", formData).then(function (res) { }).catch(function (err) { console.log(err); }) }
- 后端c#代码
[Route("yejiawei/haha")] [HttpPost] [AllowAnonymous] public void Post() { HttpFileCollection files = HttpContext.Current.Request.Files; for(int i = 0; i < files.Count; i++) { HttpPostedFile file = files[i]; file.SaveAs(@"F:\学习实验区\filereceive\" + file.FileName); } } 是不是很简单
原文地址:https://www.cnblogs.com/ye-hcj/p/8157846.html
时间: 2024-11-07 10:26:02