ASP.NET Upload feature without server-side component.
Whenever I had to implement a file upload feature in an application, I have always had to rely on third party server-side components, like AspUpload.
I have been able to work with the AspUpload component well, but it seems I won't have to anymore. ASP.NET has a built-in feature for uploading files.
Ensure the folder that you are writing to has write permission.
You can try it out below.
Note: Disabled due to detection of recent malicious activity...
(.gif & .jpg files only)
NOTE: This particular experiment allows anyone to upload gif or jpg images for purposes of showing the experiment works.
I have recently had to delete several obscene pictures from this application. This is a personal site and I ask my visitors to please respect
my domain and do NOT upload obscene or offensive pictures. I am more than happy to share my experiments and their full capabilities with the
community, but only if used properly. Thank you.
aspx.cs code
protected void Button1_Click(object sender, EventArgs e)
{
if(FileUpload.PostedFile != null) {
string FileName = FileUpload.PostedFile.FileName.Substring(
FileUpload.PostedFile.FileName.LastIndexOf("\\") + 1);
string FileType = FileUpload.PostedFile.ContentType;
int FileSize = FileUpload.PostedFile.ContentLength;
if(FileSize < 1) {
this.Label1.ForeColor = System.Drawing.Color.Red;
this.Label1.Text = "Uploading of file " + FileName + " failed";
} else if(
FileType != "image/jpeg" &&
FileType != "image/jpeg" &&
FileType != "image/gif") {
this.Label1.ForeColor = System.Drawing.Color.Red;
this.Label1.Text="MIME type of "+FileType+" is invalid.
Please use jpg or gif files only.";
} else {
FileUpload.PostedFile.SaveAs(Server.MapPath(".\\uploads\\" + FileName));
this.Image1.ImageUrl = "uploads/" + FileName;
this.Label1.ForeColor = System.Drawing.Color.Green;
this.Label1.Text = "Your file " + FileName + " of type " +
FileType + " and of size " + FileSize.ToString() +
"kb was uploaded successfully...";
}
}
}