FileUpload fl1 = (FileUpload)pnl2.FindControl("FileUpload2");
UploadImage(fl1);
sPath1 = UploadImage(fl1);
if (sPath1 == "File size cannot be larger than 2 MB")
{
lblSuccess.Text = sPath1;
return;
}
private string UploadImage(FileUpload fl)
{
string sSavePath = string.Empty;
string sSavePath1 = string.Empty;
string sFilename = string.Empty;
string msg = string.Empty;
Random RandomClass = new Random();
int RandomNumber1 = RandomClass.Next(1000, 9000);
// Set constant values
string type = fl.PostedFile.ContentType;
//code for jpg file
if (fl.HasFile && checkImageContentType(fl.PostedFile.ContentType))
{
// HttpPostedFile fl = fl.PostedFile;
if (fl.PostedFile.ContentType != "application/pdf")
{
System.Drawing.Image oImage = System.Drawing.Image.FromStream(fl.PostedFile.InputStream);
if (oImage.Width > 150 || oImage.Height > 150)
{
lblSuccess.Text = "Image is greater than 150 * 150";
completepath = lblSuccess.Text.ToString();
return completepath;
}
else
{
sSavePath = "Brochure/";
sFilename = fl.FileName;
sFilename = "Press" + Convert.ToString(Session["exid"]) + RandomNumber1 + "_" + fl.FileName;
fl.SaveAs(Server.MapPath(sSavePath + sFilename));
completepath = sSavePath + sFilename;
return completepath;
}
}
else
{
if (((Convert.ToDecimal(fl.PostedFile.ContentLength) / 1024) / 1024) > 2)
{
lblSuccess.Text = "File size cannot be larger than 2 MB";
return lblSuccess.Text;
}
else
{
sSavePath = "PDF/";
sFilename = "Press_" + Convert.ToString(Session["exid"]) + RandomNumber1 + "_" + fl.FileName;
fl.SaveAs(Server.MapPath(sSavePath + sFilename));
completepath = sSavePath + sFilename;
return completepath;
}
}
}
else
{
return msg;
}
}
public static bool checkImageContentType(string contentType)
{
//check whether the uploaded file is an image or not
if (contentType == "image/gif" || contentType == "image/jpeg" || contentType == "image/pjpeg" || contentType == "application/pdf")
return true;
else
return false;
}
<asp:FileUpload ID="FileUpload2" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="FileUpload2"
ErrorMessage="Required" Display="Dynamic">Required</asp:RequiredFieldValidator><br />
<asp:RegularExpressionValidator ID="RevDoc" runat="server" ControlToValidate="FileUpload2"
ErrorMessage="select only PDF file." ValidationExpression="^.+(.pdf)$" Display="Dynamic"><span style="color: Red">select only PDF file</span> </asp:RegularExpressionValidator>
<a id="Pdf" target="_blank" runat="server"></a><span id="err2" style="color: Red">
UploadImage(fl1);
sPath1 = UploadImage(fl1);
if (sPath1 == "File size cannot be larger than 2 MB")
{
lblSuccess.Text = sPath1;
return;
}
private string UploadImage(FileUpload fl)
{
string sSavePath = string.Empty;
string sSavePath1 = string.Empty;
string sFilename = string.Empty;
string msg = string.Empty;
Random RandomClass = new Random();
int RandomNumber1 = RandomClass.Next(1000, 9000);
// Set constant values
string type = fl.PostedFile.ContentType;
//code for jpg file
if (fl.HasFile && checkImageContentType(fl.PostedFile.ContentType))
{
// HttpPostedFile fl = fl.PostedFile;
if (fl.PostedFile.ContentType != "application/pdf")
{
System.Drawing.Image oImage = System.Drawing.Image.FromStream(fl.PostedFile.InputStream);
if (oImage.Width > 150 || oImage.Height > 150)
{
lblSuccess.Text = "Image is greater than 150 * 150";
completepath = lblSuccess.Text.ToString();
return completepath;
}
else
{
sSavePath = "Brochure/";
sFilename = fl.FileName;
sFilename = "Press" + Convert.ToString(Session["exid"]) + RandomNumber1 + "_" + fl.FileName;
fl.SaveAs(Server.MapPath(sSavePath + sFilename));
completepath = sSavePath + sFilename;
return completepath;
}
}
else
{
if (((Convert.ToDecimal(fl.PostedFile.ContentLength) / 1024) / 1024) > 2)
{
lblSuccess.Text = "File size cannot be larger than 2 MB";
return lblSuccess.Text;
}
else
{
sSavePath = "PDF/";
sFilename = "Press_" + Convert.ToString(Session["exid"]) + RandomNumber1 + "_" + fl.FileName;
fl.SaveAs(Server.MapPath(sSavePath + sFilename));
completepath = sSavePath + sFilename;
return completepath;
}
}
}
else
{
return msg;
}
}
public static bool checkImageContentType(string contentType)
{
//check whether the uploaded file is an image or not
if (contentType == "image/gif" || contentType == "image/jpeg" || contentType == "image/pjpeg" || contentType == "application/pdf")
return true;
else
return false;
}
<asp:FileUpload ID="FileUpload2" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="FileUpload2"
ErrorMessage="Required" Display="Dynamic">Required</asp:RequiredFieldValidator><br />
<asp:RegularExpressionValidator ID="RevDoc" runat="server" ControlToValidate="FileUpload2"
ErrorMessage="select only PDF file." ValidationExpression="^.+(.pdf)$" Display="Dynamic"><span style="color: Red">select only PDF file</span> </asp:RegularExpressionValidator>
<a id="Pdf" target="_blank" runat="server"></a><span id="err2" style="color: Red">



0 comments:
Post a Comment