前台:
<asp:FileUpload ID="FileUpdDiary" runat="server" />
后台:
//文件导入 string fpath = ""; string strName = this.FileUpdDiary.FileName.Trim(); System.IO.FileInfo fif; string extension = System.IO.Path.GetExtension(FileUpdDiary.FileName); //获取扩展名 try { if (!strName.Equals("") && (extension == ".xls")) { //判断上传大小小于4M if (this.FileUpdDiary.PostedFile.ContentLength < 4000000) { //判断在服务器上是否存在相同文件 fif = new System.IO.FileInfo(System.Web.HttpContext.Current.Request.MapPath("~/upload1/") + strName); if (fif.Exists) { Common.MessageBox.Show(Page, "服务器上已经存在相同文件!请重新提交。"); return; } // 保存在服务器 fpath = Server.MapPath("~/upload1/") + strName; this.FileUpdDiary.PostedFile.SaveAs(fpath); } else { Common.MessageBox.Show(Page, "上传文件最大为4M!请重新提交。"); return; } } else { Common.MessageBox.Show(Page, "上传文件扩展名.xls格式!请重新提交。"); return; } } catch (Exception ex) { Common.MessageBox.Show(Page, "文件上传失败!原因:" + ex.Message);
return; }