c# - Uploading multiple images to server. If only one image is bigger than size limit or wrong file extension then all other fails -


I have the following code that uploads multiple pictures to the server; it works well until all the selected Images are less than the size limit and the correct file extension. However, if an image does not meet those criteria only then all other images are not published. Why? I only want an image that does not meet the criteria to fail. Protected Zero Upload File Tags (Object Sender, EventArgs e) {if (UploadImages.HasFiles) {string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings ["DefaultConnection"]. ConnectionString; SqlConnection con = new SqlConnection (strConnString); Con.Open (); {Foreach (Try HttpPostedFile Upload file in. Upload Images.Posted Files) {string FileExtension = System.IO.Path.GetExtension (UploadImages.FileName); If (file extension == ".jpg") File extension == ".jpg" || FileExchange == ".GIF" || File extension == ".tiff") {UploadStatusLabel.Text = ""; Int fileSize = UploadImages.PostedFile.ContentLength; If (fileSize & lt; 150000) {UploadStatusLabel.Text = ""; String newname = System.DateTime.Now.ToString ("yyMMdd-hhmmss-") + uploadfile.philname; Upload file Sevs (system.io.path.binbin (server mappath ("/ images / editors / bg /"), new name)); Listofuploadedfiles.Text + = string.Format ("& lt; br /> uploaded successfully clear = 'all' />, newname); SqlCommand CMD = new SQL Commands (); Cmd.Connection = con; Cmd.CommandType = CommandType.Text; Cmd.CommandText = @ "Insert in Background Images (BG_Filename, IDzer) Price (@ param1, @b2b)"; Cmd.Parameters.AddWithValue ("@ 1x," new name); Cmd.Parameters.AddWithValue ("@ Ultimate 2", HttpContext.Current.User.Identity.GetUserId ()); Cmd.ExecuteNonQuery (); } Other {UploadStatusLabel.Text = "& lt; Strong & gt;" Upload file.file name + "& lt; / strong & gt; not uploaded, size exceeds 150 KB limit."; }} And {UploadStatusLabel.Text = "strong & gt;" + Uploaded file.FileName + "& lt; / strong & gt; Not uploaded. Wrong image format. Only JPG, JPG, GIF and TIFF images are accepted."; }}} Hold (pre-ex) {Response.Write ("Error in recording record in table ..." + pre-message + "insert records"); } Finally {con.Close (); Con.Dispose (); }} And {UploadStatusLabel.Text = "You have not selected a file to upload."; }}

Only work on specific items / files in your loop.

Then, this: string FileExtension = System.IO.Path.GetExtension (UploadImages.FileName); Should be: String FileExtension = System.IO.Path.GetExtension (UploadFile.FileName);

And, this: int fileSize = UploadImages.PostedFile.ContentLength; Must be: int fileSize = uploadedFile.PostedFile.ContentLength;

You are currently getting file size from UploadImages instead of a specific file in the loop, which is uploaded file.

Comments

Popular posts from this blog

php - how to change mysql_result($res, 0, "url"); to mysqli -

c# - Textbox not clickable but editable -

Matlab transpose a table vector -