- protected void AddBatButton_Click(object sender, EventArgs e)
- {
- try
- {
- MessageLabel1.Visible = true;
- if (ExcelFileUpload.HasFile)
- {
- string fileName = ExcelFileUpload.FileName;
- if (fileName.Substring(fileName.Length - 4).ToLower() != ".xls")
- {
- MessageLabel1.Text = "请选择Excel文件!";
- }
- else
- {
- float fileLength = (float)ExcelFileUpload.PostedFile.ContentLength / 1024 / 1024;
- MessageLabel1.Text = fileLength.ToString();
- if (fileLength > 3)
- {
- MessageLabel1.Text += "上传文件过大,超过了3M!";
- return;
- }
- else
- {
- try
- {
- ExcelFileUpload.SaveAs(Server.MapPath("~/Temp" + fileName));
- MessageLabel1.Text += fileName;
- return;
- }
- catch
- {
- MessageLabel1.Text += "同名的文件已经被占用,请重命名上传文件!";
- return;
- }
- }
- }
- }
- else
- {
- MessageLabel1.Text = "请选择文件!";
- return;
- }
- }
- catch
- {
- MessageLabel1.Text += "上传文件过大,超过了4M!";
- return;
- }
- }
PS:如果文件大于4M会出现问题,还没有解决。