3rd Time a charm. ASP.Net 4.6.1. I have an upload control on my form. When a user selects a file for upload then decides not to upload and remove the file the control retains the file even after being deleted from the control's text box. I do have filters on file type and file size.
protected void btnUploadPartCDoc_Click(object sender, EventArgs e) { /* ************************************************************************************************ */ /* 12/15/2016 RR - Upload file size and type are set in the web.config file. */ /* - Limit File Size to 5MB per upload/client. */ /* - File Type comma delimeted string. */ /* ************************************************************************************************ */ string providerCode = Session["providerCode"].ToString(); string loadFile = string.Empty; string maxFileSize = ConfigurationManager.AppSettings["MaxUploadFileSize"]; IEnumerable<string> allowedFileTypes = ConfigurationManager.AppSettings["AllowedUploadFileTypes"].Split(','); string saveDir = @"Document\Refugee\"; string appPath = Request.PhysicalApplicationPath.ToString(); ; string Lst3 = string.Empty; string oARegNum = string.Empty; string imagePath = string.Empty; string savePath = string.Empty; string folderPath = string.Empty; int fileSize = 0; bool isValidFileType = false; // -------------------------------------------------------- // -- To Do: // -------------------------------------------------------- // -- some providers on upload are upload this control type // -- rather than the physical image. // -------------------------------------------------------- // -- System.Web.UI.WebControls.FileUpload // -------------------------------------------------------- string fileExt = System.IO.Path.GetExtension(fleUpload.FileName).ToString().ToLower(); if (fleUpload.HasFile) { if (!fleUpload.FileName.ToString().Contains("System.Web.UI.WebControls.FileUpload")) { if (Session["workingProvider"] != null) { string provID = Session["workingProvider"].ToString(); } oARegNum = Session["oARegNum"].ToString(); imagePath = fleUpload.FileName.ToString(); imagePath = imagePath.Replace(",", ""); savePath = appPath + saveDir + imagePath; folderPath = appPath + saveDir; fileSize = fleUpload.FileBytes.Length; foreach (string uploadfileType in allowedFileTypes) { if (fileExt == uploadfileType) { isValidFileType = true; break; } } if (!isValidFileType) { hdnFieldAlert.Value = "error; ERROR! File not allowed.;Document Upload"; return; } else { if (fileSize <= Int32.Parse(maxFileSize)) { if (Utilities.IsNumeric(oARegNum)) try { hdnFieldAlert.Value = "info; Changes have been saved;Record Update"; if (hdnFieldAlert.Value != "info; Changes have been saved;Record Update") { hdnFieldAlert.Value = "error; ERROR! You must first Save Refugee Detail form.;Document Upload"; return; } if (File.Exists(savePath)) File.Delete(savePath); fleUpload.SaveAs(savePath); SqlConnection con = new SqlConnection(getConnectionString("dbConn").ToString()); SqlCommand sqlCmd = new SqlCommand("update [dbo].[provref] set oPartCDocUrl = @imgPath where oprovcode = @providerCode and [oAregNum] = @clientNum", con); sqlCmd.Parameters.Add("@imgPath", SqlDbType.VarChar).Value = imagePath; sqlCmd.Parameters.Add("@clientNum", SqlDbType.VarChar).Value = oARegNum; sqlCmd.Parameters.Add("@providerCode", SqlDbType.VarChar).Value = providerCode; con.Open(); sqlCmd.ExecuteNonQuery(); con.Close(); // **************************************************************************************** // reload form with newly uploaded image // **************************************************************************************** provRefSelectByRecordId(); hdnFieldAlert.Value = "info; Changes have been saved. File successfully uploaded.;Document Upload"; } catch (Exception ex) { hdnFieldAlert.Value = "error; ERROR! " + ex.Message.ToString() + " ;Document Upload"; } } else { hdnFieldAlert.Value = "error; ERROR! File size too large.;Document Upload"; return; } return; } } else { hdnFieldAlert.Value = "error; ERROR! Bad File. File Not Uploaded.;Document Upload"; return; } } else { hdnFieldAlert.Value = "error; ERROR! Bad File. File Not Uploaded.;Document Upload"; return; } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)