Amit

Amit

  • 1.4k
  • 212
  • 33.4k

About AsyncFileUpload in asp.net

Jul 7 2017 1:44 AM
i want to upload image file like jpg and png file using this code but asyncronus fileupload take any file when selecting. how to resolved this issue.i am writting following code .After uploading i am showing popup msg but after popup any file is uploaded.
 
ThrobberID="myThrobber" />
[Only .jpg or .Png and max. size 200KB]
 
protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
try
{
string fileExt =
System.IO.Path.GetExtension(AsyncFileUpload1.FileName);
if (fileExt == ".jpg" || fileExt == ".png" || fileExt == ".gif" || fileExt == ".jpeg")
{
if (int.Parse(e.FileSize) < int.Parse("204800"))
{
string filePath = "../assets/photo/Emp/" + e.FileName;
AsyncFileUpload1.SaveAs(MapPath(filePath));
}
else
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "clientscript", "alert('File Size must be less than 200 Kb!')", true);
}
}
else
{
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Please choose only .jpg, .png and .gif image types!')", true);
}
}
catch (Exception ex)
{
}
 
 
2)
function uploadError(sender, args) {
try {
$get("dvFileErrorInfo").style.display = 'block';
$get("dvFileInfo").style.display = 'none';
$get("<%=lblError.ClientID %>").innerHTML = "File not uploaded" + args.get_errorMessage();
}
catch (e) {
}
}
function uploadComplete(sender, args) {
try {
var fileExtension = args.get_fileName();
var gif =fileExtension.indexOf('.gif');
var png =fileExtension.indexOf('.png');
var jpg=fileExtension.indexOf('.jpg');
var jpeg=fileExtension.indexOf('.jpeg');
if (gif > 0 || png > 0 || jpg > 0 || jpeg > 0)
{
if (fileExtension.indexOf('.doc') != -1) {
$get("dvFileErrorInfo").style.display = 'block';
$get('<%=lblError.ClientID %>').innerHTML = "File extension not supported";
$get("dvFileInfo").style.display = 'none';
return;
}
if (parseInt(args.get_length()) > 204800) {
$get("dvFileErrorInfo").style.display = 'block';
$get('<%=lblError.ClientID %>').innerHTML = "File size should nto be greater than 200 kb";
$get("dvFileInfo").style.display = 'none';
return;
}
$get("dvFileErrorInfo").style.display = 'none';
$get("dvFileInfo").style.display = 'block';
$get('<%=lblSuccess.ClientID %>').innerHTML = "File uploaded successfully !!!";
}
else
{
$get("dvFileErrorInfo").style.display = 'block';
$get("<%=lblError.ClientID%>").innerHTML = "Allowed File extension are {.gif,.png,.jpg,.jpeg} supported";
$get("dvFileInfo").style.display = 'none';
return;
}

Answers (1)