Showing posts with label Image Upload Validation Sizes. Show all posts
Showing posts with label Image Upload Validation Sizes. Show all posts

Thursday, June 26, 2008

Previewing image while opening ASP.NET

Previewing image while opening

We can use the following code to preview an image while opening it using ASP.NET, C#, Java script.

<SCRIPT>
var lastfieldname = ''
function preview(i)
{
var file = i.value
if (file.length<=0) return;
var ipreview = document.all('ipreview')
ipreview.src ='file://' + file
}
img id="ipreview" border="1" height="120" src="images/spacer.gif" /><br />
adjust image:
<input id="fotofp" runat="server" name="file1" onchange="preview(this)" onfocus="preview(this)" size="25" type="file" />

Monday, June 09, 2008

Restricting File Upload size using ASP.NET C#

We can use the following code to restrict the File upload size using Asp.Net, C#.


We can set the maximum upload limit for the file, using maxRequestLength in web.config of your ASP.NET web application.

Here is the sample code for this...


protected void btnUpload_Click(object sender, System.EventArgs e)
{
string fileName = FileSelector.PostedFile.FileName;
int fN = fileName.LastIndexOf("\\");
string newFileName = fileName.Substring(fN).ToLower();

if (newFileName.EndsWith(".m4v") || newFileName.EndsWith(".mp3") || newFileName.EndsWith(".mp4") || newFileName.EndsWith(".mov"))
{
if (this.FileSelector.PostedFile.ContentLength < 210000)
{
FileSelector.PostedFile.SaveAs(
Server.MapPath("../audioFiles/") + newFileName);

lblWarn.Text = "File uploaded";
lblWarn.ForeColor = System.Drawing.Color.Green;
lblWarn.Visible = true;
}
else
{
lblWarn.Text = "File cannot exceed 350MB";
lblWarn.Visible = true;
lblWarn.ForeColor = System.Drawing.Color.Red;
}
}
else
{
lblWarn.Text = "Only extensions .m4v, .mp4, mp3, or .mov";
lblWarn.Visible = true;
lblWarn.ForeColor = System.Drawing.Color.Red;
}
}

Also default upload limit is 4MB in asp.net. You can change this value in ur web.config file.

<httpRuntime maxRequestLength="210000"/>

Include this in your webconfig file in the system.web section

You can also use FileUpload1.PostedFile.ContentLength to calculate the length of the file after it is uploaded to server.

Image size, type Upload Validation

protected void Button1_Click(object sender, EventArgs e)
{
string fileFullname = this.File1.PostedFile.FileName;


if (File1.PostedFile.ContentType.ToUpper().IndexOf("IMAGE") > -1)
{
System.Drawing.Image img = System.Drawing.Image.FromStream(File1.PostedFile.InputStream);
int Width = img.Width;
int Height = img.Height;

if (Width > 200 || Height > 200 || File1.PostedFile.ContentLength > 1024 * 1024 * 200)
{
Response.Write("<script language='javascript'>alert('your image don not confirm the with and height.!');</script>");
}
else
{
if (type == "jpg" || type == "gif" || type == "bmp" || type == "JPG" || type == "GIF")
{

string sPath = Server.MapPath("images/") + dataName + fileName;

string imgPath = "pic/" + dataName + fileName;

this.File1.PostedFile.SaveAs(sPath);

Response.Write("<script language='javascript'>alert('suceed!!!');</script>");


this.Image1.ImageUrl = imgPath;


this.Button1.Enabled = false;
this.Button1.Text = "suceed!";
this.Button1.Enabled = true;

}
else
{
Response.Write("<script language='javascript'>alert('your file type wrong!');</script>");
}
}
}
else
{

Response.Write("<script language='javascript'>alert('please choice the image to upload!');</script>");
}
}

Validations::
We can also use RegularExpressionValidator to validate the file (extensions) which you are uploading. The RE will look like this ^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.jpg|.JPG)$

and now you have to set RequiredFieldValidator for fileUpload. It stops you from proceeding further if you do not upload a file.