62,269
社区成员
发帖
与我相关
我的任务
分享
if ((FileUpload1.HasFile) == true) {
///'''上传文件''''''''''''''
string savepath = Server.MapPath("Data\\");
string filename = null;
string filekjm = null;
string ip = null;
ip = Request.ServerVariables("REMOTE_ADDR");
ip = Strings.Replace(ip, ".", "");
filekjm = System.IO.Path.GetExtension(FileUpload1.FileName);
if (filekjm != ".xls") {
Response.Write("<script>alert('请导入Excel文件!!!')</script>");
return;
}
filename = DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ip + filekjm;
filename = Strings.Replace(filename, " ", "");
savepath += filename;
FileUpload1.SaveAs(savepath);
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _uploadIT : System.Web.UI.Page
{
//protected System.Web.UI.WebControls.Button UploadButton;
// protected System.Web.UI.WebControls.Label strStatus;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["adminName"].ToString().Trim() != "")
{
if((Convert.ToInt32(Session["level"].ToString()) == 3 && Convert.ToInt32(Session["class"].ToString()) == 1)|| (Convert.ToInt32(Session["level"].ToString()) == 5))
//(Convert.ToInt32(Session["level"].ToString()) >= 4)
{
}
else
{
Response.Redirect("error.html");
}
}
else
{
Response.Redirect("login.aspx");
}
}
private Boolean SaveImages()
{
HttpFileCollection files = HttpContext.Current.Request.Files;
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
strMsg.Append("上传的文件分别是:<hr color=red>");
try
{
for (int iFile = 0; iFile < files.Count; iFile++)
{
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension, file_id;
int year = System.DateTime.Now.Year;
int month = System.DateTime.Now.Month;
int day = System.DateTime.Now.Day;
int hour = System.DateTime.Now.Hour;
int minute = System.DateTime.Now.Minute;
int second = System.DateTime.Now.Second;
int millisecond = System.DateTime.Now.Millisecond;
//string my_file_id = year.ToString() + month.ToString() + day.ToString() + hour.ToString() + minute.ToString() + second.ToString() + millisecond.ToString() + iFile.ToString();
fileName = System.IO.Path.GetFileName(postedFile.FileName);
fileExtension = System.IO.Path.GetExtension(fileName);
file_id = fileName;
if (fileName != "")
{
fileExtension = System.IO.Path.GetExtension(fileName);
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
strMsg.Append("上传文件的文件名:" + file_id + "<br>");
strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>");
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("upload/") + file_id);
}
}
strStatus.Text = strMsg.ToString();
return true;
}
catch (System.Exception Ex)
{
strStatus.Text = Ex.Message;
return false;
}
}
protected void UploadButton_Click(object sender, EventArgs e)
{
Boolean bl = SaveImages();
if (bl)
{
adminDataBase db = new adminDataBase();
int l = db.Log(Session["adminName"].ToString(), "上传文件");
Response.Write("<script>alert('上传成功!')</script>");
}
else
{
Response.Write("<script>alert('上传失败!')</script>");
}
}
}/// <summary>
/// 上传单个文件
/// </summary>
/// <param name="File1">文件控件</param>
/// <param name="sizes">上传大小限制,KB单位;为小于等于0不限</param>
/// <param name="filetype">文件后缀限定,以“|”隔开,不带"."号。为空不限</param>
/// <param name="filepath">上传到文件夹路径,为空为当前文件夹</param>
/// <param name="returnmessage">上传返回信息</param>
/// <returns>返回上传的文件名虚拟路径</returns>
public static string UploadFile(System.Web.UI.HtmlControls.HtmlInputFile File1, int sizes, string filetype, string filepath,ref string returnmessage)
{
returnmessage = "";
try
{
filepath = filepath.Trim();
if (filepath.LastIndexOf("/") != filepath.Length - 1)
{
filepath += "/";
}
string path = filepath; //保存至的文件夹
if (path == "")
{
path = "./";
}
path = System.Web.HttpContext.Current.Server.MapPath(path);
//判断是否有上传的文件
string sourcepath = File1.Value.Trim();//文件的上传的路径
if (sourcepath == "" || sourcepath == null)
{
returnmessage = "无上传文件";
return "";
}
string tfiletype = sourcepath.Substring(sourcepath.LastIndexOf(".") + 1).ToLower();
//如果要求的后缀不为空则判断上传的文件的后缀是否合适
if (filetype != "")
{
if ((filetype.ToLower() + "|").IndexOf(tfiletype + "|") < 0)
{
returnmessage = "上传的文件类型不符合设定";
return "";
}
}
if (sizes > 0)
{
int sizes2 = sizes * 1024;
//获得上传文件的大小
long strLen = File1.PostedFile.ContentLength;
//判断上传文件大小
if (strLen > sizes2)
{
returnmessage = "上传的文件不能大于" + sizes.ToString() + "KB";
return "";
}
}
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
//判断文件夹否存在,不存在则创建
if (!dir.Exists)
{
dir.Create();
}
string modifyfilename = Tools.GetId(0,12);
path = path + modifyfilename + "." + tfiletype;
while (System.IO.File.Exists(path))
{
modifyfilename = Tools.GetId(0, 12);
path = path + modifyfilename + "." + tfiletype;
}
File1.PostedFile.SaveAs(path);
filepath = filepath + modifyfilename + "." + tfiletype;
returnmessage = "上传成功";
return filepath;
}
catch
{
returnmessage = "上传出现错误";
return "";
}
}
/// <summary>
/// 上传当前文件夹下面的所有文件
/// </summary>
private void UploadCurrentFolder()
{
//获取文件夹的路径
int filenameLastindex = filename.LastIndexOf(@"\");
string dirinfo = filename.Substring(0, filenameLastindex); //文件夹的路径
DirectoryInfo dir = new DirectoryInfo(dirinfo);
//获取文件夹的名称
int dirinfoLastindex = dirinfo.LastIndexOf(@"\");
string dirname = dirinfo.Substring(0, dirinfoLastindex);
dirname = dirinfo.Replace(dirname, ""); //文件夹名称
//上传到服务器的路径
string path = Server.MapPath(@"~/upload/" + dirname + "/");
//将文件夹里的所有数据传输到服务器上
string newFilename;
string astractFileType = ".jpg.gif.exe";
foreach (FileInfo files in dir.GetFiles())
{
//获取上传文件的类型
string FileExtension = files.Extension;
//需要限制上传文件的类型
if (astractFileType.IndexOf(FileExtension) >= 0)
{
Response.Redirect("Upload.aspx?stateinfo=提示:文件夹中含有JPG,GIF,EXE类型格式的文件!不是允许的格式。部分文件没有被上传");
}
else if (files.Length > 1024000 * 150)
{
Response.Redirect("Upload.aspx?stateinfo=提示:上传的文件超过允许的上限!最大上传文件限制150M!");
}
else
{
//如果服务器上该文件夹不存在的话,创建文件夹
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
//如果文件存在就改变文件名
if (File.Exists(path + files.Name))
{
//加上日期,精确到毫秒
newFilename = DateTime.Now.ToString("yyyyMMddHHmmssff") + files.Name;
}
//如果文件名超长
else if (files.Name.Length > 50)
{
newFilename = DateTime.Now.ToString("yyyyMMddHHmmssff") + files.Name.Substring(0, 47) + "……" + files.Extension;
}
else
{
newFilename = files.Name;
}
files.CopyTo(path + newFilename);
Response.Redirect("Upload.aspx?stateinfo=上传成功!");
}
}