当前位置:首页 > Web开发 > 正文

//控制器 using System; using System.Collections.Generic; using

2024-03-31 Web开发

//控制器
using
System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.IO;//上传文件的数据流 namespace UploadImg.Controllers { public class UploadController : Controller { // GET: Upload public ActionResult Add() { return View(); } [HttpPost] public void Add(HttpPostedFileBase imgFile) { //判断是否上传图片 if (imgFile!=null) { //虚拟路径 var p = "/Content/Imgs/" + Path.GetFileName(imgFile.FileName); imgFile.SaveAs(Server.MapPath(p)); } } } }

//页面


@{
ViewBag.Title = "Add";
}

<h2>Add</h2>
@*enctype = "multipart/form-data"上传文件必需的属性*@
@using (Html.BeginForm("Add", "Upload", FormMethod.Post, new { @enctype = "multipart/form-data" }))
{
<input type="file" />
<input type="submit" value="上传" />
}

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/30211.html