C#.netmvc单文件上传 ajax上传文件
2020-12-26 00:27
标签:data fun base bsp eric scripts ica inf click 写到到数据库的模型 控制器写法 前台页面写法 C#.netmvc单文件上传 ajax上传文件 标签:data fun base bsp eric scripts ica inf click 原文地址:https://www.cnblogs.com/ataoliu/p/13381890.htmlusing System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace WebApplication1.Models
{
public class UserInfo
{
public string Id { get; set; }
public string UserName { get; set; }
public string Address { get; set; }
public HttpPostedFileBase ImgUrl { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models;
using System.IO;
namespace WebApplication1.Controllers
{
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public string Index(HttpPostedFileBase file)
{
//判断文件是否存在
if (file != null)
{
//写一个文件保存的路径
string imgpath = Server.MapPath(@"\Img\");
//判断路径是否存在,不存在则创建
if (!Directory.Exists(imgpath))
{
Directory.CreateDirectory(imgpath);
}
//给文件再起个名
string ImgName = DateTime.Now.ToString("yyyyMMddHHmmss")+"_"+file.FileName;
//把文件保存到服务器上
file.SaveAs(imgpath + ImgName);
//返回文件的位置
return @"\Img\" + ImgName;
}
return "null";
}
}
}
@{
ViewBag.Title = "Index";
}
Index
文章标题:C#.netmvc单文件上传 ajax上传文件
文章链接:http://soscw.com/index.php/essay/38228.html