using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Web;
namespace CommonHelper
{
public class UploadFile : System.Web.UI.Page
{
public UploadFile()
{
}
//错误信息
public string msg { get; set; }
public string FullName { get; set; }
//文件名称
public string FileName { get; set; }
/// <summary>
/// 文件上传
/// by wyl 20161019
/// </summary>
/// <param name="filepath">文件上传地址</param>
/// <param name="size">文件规定大小</param>
/// <param name="filetype">文件类型</param>
/// <param name="files">file上传的文件</param>
/// <param name="isrename">是否重名名</param>
/// <returns></returns>
public bool upload_file(string filepath, int size, string[] filetype, bool isrename = false)
{
filepath = Server.MapPath(filepath);
//文件夹不存在就创建
if (!Directory.Exists(filepath))
Directory.CreateDirectory(filepath);
if (HttpContext.Current.Request.Files.Count == 0)
{
msg = "文件上传失败";
return false;
}
msg = "上传成功";
var file = HttpContext.Current.Request.Files[0];
if (file.ContentLength == 0)
{
msg = "文件大小为0";
return false;
}
if (file.ContentLength > size * 1024)
{
msg = "文件超出指定大小";
return false;
}
var filex = HttpContext.Current.Request.Files[0];
string fileExt = Path.GetExtension(filex.FileName).ToLower();
if (filetype.Count(a => a == fileExt) < 1)
{
msg = "文件类型不支持";
return false;
}
if (isrename)
FileName = DateTime.Now.ToString("yyyyMMddHHmmss") + fileExt;
FileName = filex.FileName;
FullName = Path.Combine(filepath, FileName);
file.SaveAs(FullName);
return true;
}
}
}
<div class="content"> <form method="post" target="hidden_frame" enctype="multipart/form-data" action="/CustomFrom/FormDesign/FileUpload" name="form"> <input class="m input" name="fileName" type="file"> <input class="btn file-input" value="提交..." name="F2" type="submit"> <iframe id="hidden_frame" name="F2" style="display: none"> <html> <head></head> <body></body> </html> </iframe> </form> </div>
[HttpPost]
public ActionResult FileUpload()
{
//从配置文件中获取支持上传文件格式
string[] fileType = ConfigurationManager.AppSettings["fileType"].Split('|');
//上传文件路径
string strPath = ConfigurationManager.AppSettings["strPath"];
UploadFile file= new UploadFile();
bool flag = file.upload_file(strPath, 25000, fileType);
return Content("<script>window.alert('" + file.msg + "');window.top.close()</script>");
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有