aspx.cs代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {
} protected void Button1_Click(object sender, EventArgs e) { //获取文件的后缀名 string bb = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower(); //文件上传后存放的路径 string image = Server.MapPath("~/File/image/");//图片 string video = Server.MapPath(" ~/File/Video/");//视频 string file = Server.MapPath("~/File/File/");//文档
//文件上传服务器时存放的路径 string serverPath = PactFilePath+"\\"; // 创建服务器上传路径 if (!Directory.Exists(serverPath)) { Directory.CreateDirectory(serverPath); } if (flu_filepath.HasFile) {
//图片、视频、文件格式自己设定,为了合成二维数组都写了6种格式 string[] Image={ ".gif", ".png", ".jpeg", ".jpg","bmp",".pic"};
string[] Video = { ".AVI", ".wma", ".rmvb", ".rm", ".flash", ".mp4"};
string[] File = { ".doc", ".txt", ".wps", ".pdf", ".rar", ".zip" };
string[,] arr = new string[3, 6] { { ".doc", ".txt", ".wps", ".pdf", ".rar", ".zip" }, { ".gif", ".png", ".jpeg", ".jpg", "bmp", ".pic" }, { ".AVI", ".wma", ".rmvb", ".rm", ".flash", ".mp4" } }; if (FileUpload1.HasFile) { bool break_flag = false; for (int i = 0; i < 3; i++) { for (int j = 0; j < 6; j++) { if ((i == 0) && (bb == arr[0, j].ToString())) { //将文件保存到指定的位置 FileUpload1.PostedFile.SaveAs(file + FileUpload1.FileName); break_flag = true; break; } else if ((i == 1) && (bb == arr[1, j].ToString())) { FileUpload1.PostedFile.SaveAs(image + FileUpload1.FileName); break_flag = true; break;
}
else if ((i == 2) && (bb == arr[2, j].ToString())) { FileUpload1.PostedFile.SaveAs(video + FileUpload1.FileName); break_flag = true; break; } } } if (break_flag == true) { Response.Write("<script>alert('上传成功!')</script>");
} else { Response.Write("<script>alert('上传文件格式错误!')</script>"); }
} else { Response.Write("<script>alert('请选择上传文件!')<script>"); } } }
HTML代码
<body> <form /> </div>
</form> </body>
web.config 如果上传的文件过大,记得修改配置文件,C#默认是2MB,最大可调到2097151KB大约是2G,executionTimeout设置上传时间
<configuration> <system.web> <httpRuntime maxRequestLength="2097151" executionTimeout="120"/> </system.web> </configuration>
下载文件的方法:
新建一个ASPX页面
/// <summary> /// 下载文件 /// <param name="file">下载的文件夹</param> /// <param name="filePath">文件路径</param> /// <param name="fileName">下载的文件全名</param> /// </summary> public static void DownLoadPactFile(string fileName) { string filePath = string.Empty; filePath = PactFilePath +"\\"+ fileName;
if (File.Exists(filePath)) { System.IO.FileStream dFileSM; System.IO.BinaryReader dFileBR;
dFileSM = new System.IO.FileStream(HttpUtility.UrlDecode(filePath, System.Text.Encoding.UTF8), System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite); dFileBR = new System.IO.BinaryReader(dFileSM);
//重新处理文件名 string[] name = HttpUtility.UrlDecode(filePath, System.Text.Encoding.UTF8).Split('\\'); //编码文件名 string strEdFileName = HttpUtility.UrlEncode(name[name.Length - 1], System.Text.Encoding.UTF8);
//变更Http头信息 HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Charset = "utf-8"; HttpContext.Current.Response.AddHeader("connection", "keep-alive"); HttpContext.Current.Response.ContentType = "application/octet-stream"; HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + strEdFileName); HttpContext.Current.Response.AddHeader("content-length", dFileSM.Length.ToString());
//当生成的文件大小为0字节时,打开的文件为空 if (dFileSM.Length != 0) { HttpContext.Current.Response.BinaryWrite(dFileBR.ReadBytes((int)dFileSM.Length)); }
// 对象释放 dFileBR.Close(); dFileSM.Close(); dFileBR = null; dFileSM = null; }
|
请发表评论