protected void btn_Upload_Click(object sender, EventArgs e) { if (Page.IsValid && File1.HasFile) { string fileName = "images/" + File1.FileName; string filePath = MapPath(fileName); File1.SaveAs(filePath); Image1.ImageUrl = fileName; } }
protected void btnupload_Click(object sender, EventArgs e) { if (File1.HasFile) { try { if (File1.PostedFile.ContentType == "image/jpeg") { if (File1.PostedFile.ContentLength < 8512000) { string filename = System.IO.Path.GetFileName(File1.FileName); File1.SaveAs(Server.MapPath("~/FileUploads/" + filename)); Label1.Text = "File uploaded successfully!"; } else { Label1.Text = "File maximum size is 500 Kb"; } } else { Label1.Text = "Only JPEG files are accepted!"; } } catch (Exception exc) { Label1.Text = "The file could not be uploaded. The following error occured: " + exc.Message; } } }
protected void SaveButton_Click(object sender, EventArgs e) { string strFullName = string.Empty; //, strUrl, strContentType, strSize CoursewareBLL coursewareBLL = new CoursewareBLL(); if (ViewState["AddFlag"].ToString() == "1") //新增 { strFullName = File1.FileName; //直接取得文件名 if (!string.IsNullOrEmpty(strFullName)) { FileInfo fi = new FileInfo(File1.FileName); //判断只能上传.flv与.swf格式 //if (fi.Extension.ToLower() != ".flv" && fi.Extension.ToLower() != ".swf") //{ // SessionSet.PageMessage = "文件格式应为FLV或SWF视频! 现在格式为:" + fi.Extension.ToLower(); // return; //} if (fi.Name.Replace(fi.Extension, "") != txtCoursewareName.Text) { SessionSet.PageMessage = "上传文件名与课件名不一致,请确认上传文件名!"; return; } } RailExam.Model.Courseware courseware = new RailExam.Model.Courseware(); courseware.CoursewareName = txtCoursewareName.Text; courseware.CoursewareTypeID = int.Parse(hfCoursewareTypeID.Value); courseware.ProvideOrg = SessionSet.OrganizationID; courseware.PublishDate = DateTime.Parse(datePublishDate.DateValue.ToString()); courseware.Authors = txtAuthors.Text; courseware.Revisers = txtRevisers.Text; courseware.KeyWord = txtKeyWord.Text; courseware.Description = txtDescription.Text; courseware.Memo = txtMemo.Text; courseware.IsGroupLearder = Convert.ToInt32(ddlIsGroup.SelectedValue); courseware.TechnicianTypeID = Convert.ToInt32(ddlTech.SelectedValue); ArrayList al = new ArrayList(); courseware.OrgIDAL = GetOrg(tvOrg.Nodes, al); ArrayList al1 = new ArrayList(); foreach (TreeViewNode tn in tvPost.Nodes) { if (tn.Checked) { al1.Add(tn.ID); } if (tn.Nodes.Count > 0) { foreach (TreeViewNode tns in tn.Nodes) { if (tns.Checked) { al1.Add(tns.ID); } if (tns.Nodes.Count > 0) { foreach (TreeViewNode tnss in tns.Nodes) { if (tnss.Checked) { al1.Add(tnss.ID); } } } } } } courseware.PostIDAL = al1; ArrayList trainTypeIDList = new ArrayList(); string[] strType = hfTrainTypeID.Value.Split(','); for (int i = 0; i < strType.Length; i++) { if (!string.IsNullOrEmpty(strType[i])) { trainTypeIDList.Add(strType[i]); } } courseware.TrainTypeIDAL = trainTypeIDList; int nCoursewareID = coursewareBLL.AddCourseware(courseware); if (!string.IsNullOrEmpty(strFullName)) { //strUrl = File1.PostedFile.FileName;//先取得全部的上传文件路径个名字,然后再利用SubString方法来得到用户名,现在看来是没有必要了 //strContentType = File1.PostedFile.ContentType;//获取文件MIME内容类型 //strFileType = strFullName.Substring(strFullName.LastIndexOf(".") + 1);//获取文件名字 . 后面的字符作为文件类型 //strSize = File1.PostedFile.ContentLength.ToString(); Directory.CreateDirectory(Server.MapPath("../Online/Courseware/" + nCoursewareID)); File1.SaveAs(Server.MapPath("../Online/Courseware/" + nCoursewareID + "/") + strFullName); //将文件保存在跟目录的UP文件夹下 courseware.CoursewareID = nCoursewareID; courseware.Url = "/RailExamBao/Online/Courseware/" + nCoursewareID + "/" + strFullName; coursewareBLL.UpdateCourseware(courseware); } Response.Write("<script>window.opener.form1.Refresh.value='true';window.opener.form1.submit();window.close();</script>"); } else //修改 { string strCoursewareID = Request.QueryString.Get("id"); RailExam.Model.Courseware courseware = coursewareBLL.GetCourseware(Convert.ToInt32(strCoursewareID)); courseware.CoursewareID = int.Parse(strCoursewareID); courseware.CoursewareName = txtCoursewareName.Text; courseware.CoursewareTypeID = int.Parse(hfCoursewareTypeID.Value); courseware.ProvideOrg = int.Parse(hfProvideOrgID.Value); courseware.PublishDate = DateTime.Parse(datePublishDate.DateValue.ToString()); courseware.Authors = txtAuthors.Text; courseware.Revisers = txtRevisers.Text; courseware.KeyWord = txtKeyWord.Text; courseware.Description = txtDescription.Text; courseware.IsGroupLearder = Convert.ToInt32(ddlIsGroup.SelectedValue); courseware.TechnicianTypeID = Convert.ToInt32(ddlTech.SelectedValue); courseware.OrderIndex = Convert.ToInt32(hfOrderIndex.Value); strFullName = File1.FileName; //直接取得文件名 //strUrl = File1.PostedFile.FileName;//先取得全部的上传文件路径个名字,然后再利用SubString方法来得到用户名,现在看来是没有必要了 //strContentType = File1.PostedFile.ContentType;//获取文件MIME内容类型 //strFileType = strFullName.Substring(strFullName.LastIndexOf(".") + 1);//获取文件名字 . 后面的字符作为文件类型 //strSize = File1.PostedFile.ContentLength.ToString(); if (!string.IsNullOrEmpty(strFullName)) { FileInfo fi = new FileInfo(File1.FileName); //如果是武汉,就判断只能上传.flv与.swf格式 //if (fi.Extension.ToLower() != ".flv" && fi.Extension.ToLower() != ".swf") //{ // SessionSet.PageMessage = "文件格式应为FLV或SWF视频! 现在格式为:" + fi.Extension.ToLower(); // return; //} if (fi.Name.Replace(fi.Extension, "") != txtCoursewareName.Text) { SessionSet.PageMessage = "上传文件名与课件名不一致,请确认上传文件名!"; return; } if (File.Exists(Server.MapPath(courseware.Url))) { //string[] filelist = Directory.GetFileSystemEntries(Server.MapPath("../Online/Courseware/" + courseware.CoursewareID + "/")); //foreach (string file in filelist) //{ // if(!Directory.Exists(file)) // { // File.Delete(file); // } //} File.Delete(Server.MapPath(courseware.Url)); } if (!Directory.Exists(Server.MapPath("../Online/Courseware/" + strCoursewareID))) { Directory.CreateDirectory(Server.MapPath("../Online/Courseware/" + strCoursewareID)); } File1.SaveAs(Server.MapPath("../Online/Courseware/" + strCoursewareID + "/") + strFullName); //将文件保存在跟目录的UP文件夹下 courseware.Url = "/RailExamBao/Online/Courseware/" + strCoursewareID + "/" + strFullName; } courseware.Memo = txtMemo.Text; ArrayList al = new ArrayList(); courseware.OrgIDAL = GetOrg(tvOrg.Nodes, al); ArrayList al1 = new ArrayList(); foreach (TreeViewNode tn in tvPost.Nodes) { if (tn.Checked) { al1.Add(tn.ID); } if (tn.Nodes.Count > 0) { foreach (TreeViewNode tns in tn.Nodes) { if (tns.Checked) { al1.Add(tns.ID); } if (tns.Nodes.Count > 0) { foreach (TreeViewNode tnss in tns.Nodes) { if (tnss.Checked) { al1.Add(tnss.ID); } } } } } } courseware.PostIDAL = al1; ArrayList trainTypeIDList = new ArrayList(); string[] strType = hfTrainTypeID.Value.Split(','); for (int i = 0; i < strType.Length; i++) { if (!string.IsNullOrEmpty(strType[i])) { trainTypeIDList.Add(strType[i]); } } courseware.TrainTypeIDAL = trainTypeIDList; coursewareBLL.UpdateCourseware(courseware); Response.Write("<script>window.opener.form1.Refresh.value='true';window.opener.form1.submit();window.close();</script>"); } }
protected void UpdateFinally_Click(object sender, EventArgs e) { if (UpdateFinally.Text == "UPDATE COOK") { Button insert = (Button)sender; if (insert != null) { KuvariPozicijaUpdate updateKuvari = new KuvariPozicijaUpdate { kuvarPozicija = new KuvariPozicijaItem { IdKuvar = Convert.ToInt32(HiddenField1.Value), Ime = TextBox1.Text, idPozicije = Convert.ToInt32(DropDownListPozicije.SelectedValue), Slika = "" } }; if ((File1.PostedFile != null) && (File1.PostedFile.ContentLength > 0)) { Random rnd = new Random(); string uid = rnd.Next(10000000).ToString(); string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName); string SaveLocation = Server.MapPath("~/images/") + "" + uid + fn; string SaveLocationMala = Server.MapPath("~/images/") + "" + uid + fn + "Mala"; updateKuvari.kuvarPozicija.Slika = "~/images/" + uid + fn; string fileExtention = File1.PostedFile.ContentType; int fileLenght = File1.PostedFile.ContentLength; if (fileExtention == "image/png" || fileExtention == "image/jpeg" || fileExtention == "image/x-png") { File1.SaveAs(SaveLocation); System.Drawing.Bitmap bmpPostedImage = new System.Drawing.Bitmap(File1.PostedFile.InputStream); System.Drawing.Image objImage = ScaleImage(bmpPostedImage, 81); // Saving image in jpeg format objImage.Save(SaveLocationMala); updateKuvari.kuvarPozicija.SlikaMala = "~/images/" + uid + fn + "Mala"; } } OperationResult result = this.menager.execute(updateKuvari); LabelResult.Visible = true; if (result.Success) { LabelResult.Text = "Successfully updated"; UpdateFinally.Text = "INSERT COOK"; TextBox1.Text = ""; fillKuvari(); return; } else { LabelResult.Text = "Update failed"; LabelResult.ForeColor = System.Drawing.ColorTranslator.FromHtml("#d9534f"); return; } } } if (UpdateFinally.Text == "INSERT COOK") { KuvariPozicijaInsert insertKuvari = new KuvariPozicijaInsert { kuvarPozicija = new KuvariPozicijaItem { Ime = TextBox1.Text, idPozicije = Convert.ToInt32(DropDownListPozicije.SelectedValue), Slika = "" } }; if ((File1.PostedFile != null) && (File1.PostedFile.ContentLength > 0)) { Random rnd = new Random(); string uid = rnd.Next(10000000).ToString(); string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName); string SaveLocation = Server.MapPath("~/images/") + "" + uid + fn; string SaveLocationMala = Server.MapPath("~/images/") + "" + uid + fn + "Mala"; insertKuvari.kuvarPozicija.Slika = "~/images/" + uid + fn; string fileExtention = File1.PostedFile.ContentType; int fileLenght = File1.PostedFile.ContentLength; if (fileExtention == "image/png" || fileExtention == "image/jpeg" || fileExtention == "image/x-png") { File1.SaveAs(SaveLocation); System.Drawing.Bitmap bmpPostedImage = new System.Drawing.Bitmap(File1.PostedFile.InputStream); System.Drawing.Image objImage = ScaleImage(bmpPostedImage, 81); objImage.Save(SaveLocationMala); insertKuvari.kuvarPozicija.SlikaMala = "~/images/" + uid + fn + "Mala"; } } OperationResult resultInsert = this.menager.execute(insertKuvari); if (resultInsert.Success) { LabelResult.Visible = true; LabelResult.Text = "Successfully inserted"; TextBox1.Text = ""; fillKuvari(); } } }