1 前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="upload" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head />
</form>
</body>
</html>
2 后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 上传处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnUpload_Click(object sender, EventArgs e)
{
string filepath = Request.Form["fileUpload"];
//上传文件保存路径
string savePath = Server.MapPath("UploadFiles") + "\\";
//提供对客户端上载文件的访问
HttpFileCollection uploadFiles = System.Web.HttpContext.Current.Request.Files;
try
{
for (int i = 0; i < uploadFiles.Count; i++)
{
System.Web.HttpPostedFile postedFile = uploadFiles[i];
string fileName = postedFile.FileName;//完整的路径
fileName = System.IO.Path.GetFileName(postedFile.FileName); //获取到名称
string fileExtension = System.IO.Path.GetExtension(fileName);//文件的扩展名称
string type = fileName.Substring(fileName.LastIndexOf(".") + 1); //类型
if (uploadFiles[i].ContentLength > 0)
uploadFiles[i].SaveAs(savePath + fileName);
}
}
catch (System.Exception Ex)
{
Response.Write(Ex);
}
}
}