Thursday, April 30, 2009

FileUpload control in ASP.NET web form using c#

FileUpload is very simple control allows user to select file and upload.
You can easily handle that with very little coding and ease. I have given the full code behind for this functionality.
First create a aspx page with 3 controls
FileUpload: rename it to FileUpload1
Label: rename it to lblMsg
Button: rename it to btnAdd
Copy this code in your code behind file inside the class declaration. As you can see we are simply calling theSaveUploadedFile() function to save the uploaded file. you can check for the particular extension within the function right now we are checking for only 3 extensions. You can change it in sAllowedExt string.
Here is the code behind stuff:

String UPLOAD_PATH = "";
int MAXSIZE = 5 * 1024 * 1024; // max 5 MB
String sAllowedExt = ",.doc,.pdf,.xls,"; // make sure whatever extension you enter, string ends with comma
protected void Page_Load(object sender, EventArgs e)
{
btnUpload.Attributes.Add("onclick", "return checkFile();");
UPLOAD_PATH =
Server.MapPath("~/UploadedDocs") + "/";
}
private void setMsg(String sMsg)
{
lblMsg.Text = sMsg;
}
protected void btnAdd_Click(object sender, EventArgs e)
{
try
{
SaveUploadedFile();
}
catch (Exception ex)
{
//Response.Write("Error: " + ex.Message);
setMsg("File could not be uploaded successfully.");
}
}

private void SaveUploadedFile()
{
if (FileUpload1.HasFile)
{
String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
String validExt = ;
if (validExt.IndexOf("," + fileExtension + ",") != -1)
{
if (FileUpload1.FileBytes.Length <= MAXSIZE)
{
FileUpload1.PostedFile.SaveAs(UPLOAD_PATH + FileUpload1.FileName);
setMsg("File uploaded successfully.");
}
else
{
setMsg("File size exceeded the permitted limit.");
}
}
else
{
setMsg("This file type is not accepted.");
}
}
}
Once the user uploads the file, it's availabe using FileUpload.PostedFile.SaveAs() method. You can then directly save that file to particular folder with new name. We are checking FileUpload1.HasFileto make sure we only process if a file is uploaded.

Wednesday, April 29, 2009

ASP.NET event firing twice

First Solution:
This seems to be a very common problem where by your event handler method gets run twice.
It is caused by VS.NET inserting 2 wireup of the event handler:
  • once in the aspx (HTML) eg:
  • and once in the VS.NET generated section (InitializeComponent) eg:
this.btnTest.Click += new System.EventHandler(this.btnTest_Click);

The easiest solution, imo, is to simply remove the "OnClick..." HTML markup from the .aspx page.
e.g: change this: < asp:Button ID="btnTest" runat="server" Text="Test" OnClick="btnTest_Click" >
To this: < asp:Button ID="btnTest" runat="server" Text="Test" >
Rebuild, and bingo! - 1 event per click.

Second Solution:

I had the same problem, but none of the solutions above solved my problem..I was using HTML controls on my aspx page like < button runat="server" id="btnNext" type="submit" onserverclick="btnNext_Click" > The solution for me was to change the type="submit" to type="button".

Large file uploads in ASP.NET

Uploading files via the FileUpload control gets tricky with big files. The default maximum filesize is 4MB - this is done to prevent denial of service attacks in which an attacker submitted one or more huge files which overwhelmed server resources. If a user uploads a file larger than 4MB, they'll get an error message: "Maximum request length exceeded."

Increasing the Maximum Upload Size

The 4MB default is set in machine.config, but you can override it in you web.config. For instance, to expand the upload limit to 20MB, you'd do this:

<system.web>
<httpRuntime executionTimeout="240" maxRequestLength="20480"/>
</system.web>

Since the maximum request size limit is there to protect your site, it's best to expand the file-size limit for specific directories rather than your entire application. That's possible since the web.config allows for cascading overrides. You can add a web.config file to your folder which just contains the above, or you can use the tag in your main web.config to achieve the same effect:

<location path="Upload">
<system.web>
<httpRuntime executionTimeout="110" maxRequestLength="20000" />
</system.web>
</location>
Heaven - it is ok but try to earn money at hellishdollars.com !