⚠️注意:图片不可大于3707.6kb
效果图:
public class UploadController : Controller
{
public ActionResult upload()
{
return View();
}
#region 文件处理
/// <summary>
/// 上传附件 到临时文件夹
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
///
[HttpPost]
public ActionResult UpAttachment()
{
HttpPostedFileBase httpPostedFileBase = Request.Files[0];//获取二进制图片文件流
if (httpPostedFileBase != null)
{
try
{
ControllerContext.HttpContext.Request.ContentEncoding = Encoding.GetEncoding("UTF-8");
ControllerContext.HttpContext.Response.Charset = "UTF-8";
string fileName = Path.GetFileName(httpPostedFileBase.FileName);//原始文件名称
string fileExtension = Path.GetExtension(fileName);//文件扩展名
byte[] fileData = ReadFileBytes(httpPostedFileBase);//文件流转化为二进制字节
string result = SaveFile(fileExtension, fileData);//文件保存
if (string.IsNullOrEmpty(result))
{
return Json(new {
isSuccess = false, path = "", errorMsg = "上传文件失败" });
}
return Json(new {
isSuccess = true, path = result });
}
catch (Exc