Answered by:
upload file with size limit 1 mb else flash message that size more than 1 mb

Question
-
User364480375 posted
string image1 = null; if (fileUpload1.HasFile) { image1 = fileUpload1.FileName; string[] ValidFileExtensions = new string[] { ".pdf", ".doc", ".docx" }; fi1 = new FileInfo(fileUpload1.FileName); ext1 = fi1.Extension; // loop over the array of valid file extensions to compare them with uploaded file foreach (string extension in ValidFileExtensions) { if (ext1 == extension) { flag1 = true; } } } else { image1 = "nofile.pdf"; flag1 = true; ext1 = ".pdf"; }
masters I have to upload 1 mb file. but at user end if user try to upload more than 1 mb file then 1 message should be displayed that size is more 1 mb.
with above code how to do it.
Thursday, September 1, 2016 10:53 AM
Answers
-
User632428103 posted
Hello,
why you don't check the file size in javascript ?
var oFile = document.getElementById("fileUpload").files[0]; //2 mega it's enough i think if (oFile.size > 2097152) { alert("THE FILE SELECTED IS TOO GROWTH ..."); return; }
hope this help
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, September 1, 2016 11:47 AM -
User283571144 posted
Hi mike4u,
i tried above code but getting error web page not available.According to your description, I couldn’t find why you get the error web page not available error.
Could you please post more codes about your HTML and upload file event codes?
If you could post more relevant codes, It will be more easily for us to reproduce your problem.
Besides, if you want to check the file size before file upload, I suggest you could use CustomValidator control.
More details, you could refer to follow codes and link:
Html:
<div> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick="btnUpload_Click" /><br /> <asp:CustomValidator ID="CustomValidator1" OnServerValidate="ValidateFileSize" ForeColor="Red" runat="server" /> </div>
Code-behind:
protected void ValidateFileSize(object sender, ServerValidateEventArgs e) { decimal size = Math.Round(((decimal)FileUpload1.PostedFile.ContentLength / (decimal)1024), 2); if (size > 1024) { CustomValidator1.ErrorMessage = "File size must not exceed 1MB."; e.IsValid = false; } } protected void btnUpload_Click(object sender, EventArgs e) { String path = Server.MapPath("~/Images/"); FileUpload1.SaveAs(path + FileUpload1.FileName); }
Link: How to: Validate with a Custom Function for ASP.NET Server Controls
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, September 2, 2016 5:34 AM
All replies
-
User364480375 posted
string image1 = null; if (fileUpload1.HasFile) { int fileSize = fileUpload1.PostedFile.ContentLength; if (fileSize < 2100000) { image1 = fileUpload1.FileName; string[] ValidFileExtensions = new string[] { ".pdf", ".doc", ".docx" }; fi1 = new FileInfo(fileUpload1.FileName); ext1 = fi1.Extension; // loop over the array of valid file extensions to compare them with uploaded file foreach (string extension in ValidFileExtensions) { if (ext1 == extension) { flag1 = true; } } } else { // Notify the user why their file was not uploaded. ClientScript.RegisterStartupScript(this.GetType(), "Alert", string.Format("alert('file size is more than 1 MB');"), true); }
i tried above code but getting error web page not available.
Thursday, September 1, 2016 11:10 AM -
User632428103 posted
Hello,
why you don't check the file size in javascript ?
var oFile = document.getElementById("fileUpload").files[0]; //2 mega it's enough i think if (oFile.size > 2097152) { alert("THE FILE SELECTED IS TOO GROWTH ..."); return; }
hope this help
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, September 1, 2016 11:47 AM -
User283571144 posted
Hi mike4u,
i tried above code but getting error web page not available.According to your description, I couldn’t find why you get the error web page not available error.
Could you please post more codes about your HTML and upload file event codes?
If you could post more relevant codes, It will be more easily for us to reproduce your problem.
Besides, if you want to check the file size before file upload, I suggest you could use CustomValidator control.
More details, you could refer to follow codes and link:
Html:
<div> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick="btnUpload_Click" /><br /> <asp:CustomValidator ID="CustomValidator1" OnServerValidate="ValidateFileSize" ForeColor="Red" runat="server" /> </div>
Code-behind:
protected void ValidateFileSize(object sender, ServerValidateEventArgs e) { decimal size = Math.Round(((decimal)FileUpload1.PostedFile.ContentLength / (decimal)1024), 2); if (size > 1024) { CustomValidator1.ErrorMessage = "File size must not exceed 1MB."; e.IsValid = false; } } protected void btnUpload_Click(object sender, EventArgs e) { String path = Server.MapPath("~/Images/"); FileUpload1.SaveAs(path + FileUpload1.FileName); }
Link: How to: Validate with a Custom Function for ASP.NET Server Controls
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, September 2, 2016 5:34 AM