protected void btnDel_Click(object sender, EventArgs e) { if (this.selectedFiles.SelectedIndex == -1) { this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert(\"请选择要删除的文件!\");", true); return; } SSpaceBC spaceBC = new SSpaceBC(); try { spaceBC.DeleteFile(Convert.ToInt32(this.selectedFiles.Value)); } catch (Exception ex) { this.ClientScript.RegisterStartupScript(this.GetType(), "ShowErr", "ShowErr(\"" + Server.UrlEncode(ex.Message) + "\",1);", true); return; } finally { spaceBC.Dispose(); } this.selectedFiles.Items.Remove(this.selectedFiles.Items[this.selectedFiles.SelectedIndex]); }
private SFileData UpImg(HttpPostedFile postedFile) { //判断上传控件是否为空 if (postedFile.FileName == "") { this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert(\"请首先选择一个图片文件!\");", true); return null; } SFileData model = new SFileData(); SSpaceBC spaceBC = new SSpaceBC(); try { //判断文件格式(0无限制、1图片) FileInfo file = new FileInfo(postedFile.FileName); string extension = file.Extension; SCommBB commBB = new SCommBB(); try { //查看文件格式是否存在与系统表“图片文件格式”中 if (commBB.Query("select 1 from SImageFileType where fileExtension='" + extension + "'").Tables[0].Rows.Count == 0) { this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert(\"本次附件上传要求为图片格式文件,请重新选择!\");", true); return null; } } finally { commBB.Dispose(); } int fileId = spaceBC.UploadFile(model, postedFile, "upFile", this.currentUser.empId, false); model.fileId = fileId; return model; } finally { spaceBC.Dispose(); } }
protected void btnUp_Click(object sender, EventArgs e) { this.lblMsg.Text = ""; this.isUpSucess = false; if (this.SingleLimit == "1" && this.selectedFiles.Items.Count > 0) { this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert(\"本次文件上传只允许选择单个文件!\");", true); return; } int userId = this.currentUser.empId; int fileID = 0; string preFileNm = ""; SFileData model = new SFileData(); SSpaceBC spaceBC = new SSpaceBC(); try { //判断文件格式(0无限制、1图片) switch (this.FileType) { case "0": break; case "1": FileInfo file = new FileInfo(this.File1.PostedFile.FileName); string extension = file.Extension; //查看文件格式是否存在与系统表“图片文件格式”中 SCommBB commBB = new SCommBB(); try { if (commBB.Query("select 1 from SImageFileType where fileExtension='" + extension + "'").Tables[0].Rows.Count == 0) { this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert(\"本次附件上传要求为图片格式文件,请重新选择!\");", true); return; } } finally { commBB.Dispose(); } break; default: break; } fileID = spaceBC.UploadFile(model, this.File1.PostedFile, userId, this.FileType == "1"); if (fileID != 0) { preFileNm = model.preFileNm; } ListItem item = new ListItem(); item.Text = preFileNm; item.Value = fileID.ToString(); this.selectedFiles.Items.Add(item); this.isUpSucess = true; } catch (Exception ex) { this.ClientScript.RegisterStartupScript(this.GetType(), "ShowErr", "ShowErr(\"" + Server.UrlEncode(ex.Message) + "\",1);", true); return; } finally { spaceBC.Dispose(); } }
/// <summary> /// 上传文件 /// </summary> /// <returns>上传后的文件名称</returns> public string SaveFile(HttpPostedFile file) { if (file.FileName == "") return ""; SFileData model = new SFileData(); SSpaceBC spaceBC = new SSpaceBC(); try { spaceBC.UploadFile(model, file, "upFile", this.currentUser.empId, false); } catch (Exception ex) { this.ClientScript.RegisterStartupScript(this.GetType(), "ShowErr", "ShowErr(\"" + Server.UrlEncode(ex.Message) + "\",3);", true); } finally { spaceBC.Dispose(); } if (model != null) { return model.absolutPath + model.preFileNm; } else { return ""; } }