@{ ViewBag.Title = "Home Page"; Layout = null; } <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <!-- import CSS --> <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"> </head> <body> <div id="app"> <el-dialog :visible.sync="visible" title="Hello world"> <p>Try Element</p> <el-upload class="upload-demo" action="@Url.Action("upload")" :on-preview="handlePreview" :on-remove="handleRemove" :before-remove="beforeRemove" :limit="3" :on-exceed="handleExceed" :on-success="handleSuccess" :on-error="handleError" :on-progress="handleProgress" :file-list="fileList"> <el-button size="small" type="primary">点击上传</el-button> <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div> </el-upload> </el-dialog> </div> </body> <!-- import Vue before Element --> <script src="https://unpkg.com/vue/dist/vue.js"></script> <!-- import JavaScript --> <script src="https://unpkg.com/element-ui/lib/index.js"></script> <script> dd=new Vue({ el: '#app', data: function() { return { visible:true, fileList: [] } }, methods: { handleRemove(file, fileList) { console.log(file, fileList); }, handlePreview(file) { console.log(file); }, handleExceed(files, fileList) { this.$message.warning(`当前限制选择 3 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`); }, beforeRemove(file, fileList) { return this.$confirm(`确定移除 ${file.name}?`); }, handleSuccess(response, file, fileList) { if (response != "") { this.fileList = []; this.$message.warning(response); } else { this.$message.success("Save Success"); self.visible = false; } console.log("Save Success", file, fileList,"suu"); }, handleError(err, file, fileList) { console.log(err, file, fileList, "err"); this.$message.warning(err); }, handleProgress(event, file, fileList) { console.log(event, file, fileList,"event"); } } }) </script> </html>
NET
public ActionResult upload(HttpPostedFileBase file) { String uploadSectionDir = Path.Combine("Upload", DateTime.Now.ToString("yyyyMMdd")); string uploadDir = Path.Combine(HttpRuntime.AppDomainAppPath, uploadSectionDir); if (!Directory.Exists(uploadDir)) { Directory.CreateDirectory(uploadDir); } string fileName = Guid.NewGuid().ToString("N") + Path.GetExtension(file.FileName); string filePath = Path.Combine(uploadDir, fileName); file.SaveAs(filePath); return Json(""); return Json("error don't upload"); }
NET CORE 3.1
public async Task<IActionResult> uploadAsync(IFormFile file) { String uploadSectionDir = Path.Combine("Upload", DateTime.Now.ToString("yyyyMMdd")); if (!Directory.Exists(uploadSectionDir)) { Directory.CreateDirectory(uploadSectionDir); } string fileName = Guid.NewGuid().ToString("N") + Path.GetExtension(file.FileName); string filePath = Path.Combine(uploadSectionDir, fileName); if (file.Length > 0) { using (var stream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(stream); } } return Json("error don't upload"); return Ok(""); }