Saturday, March 24, 2012

Problem during Uploading the file

Hi

The Problem that am facing is

1)The path of the file that is saving in the database is "Path of the Server" + "images/"Up" of the path variable. I dont know why.

2) When i click on the Save Button, the code says File Uploaded but the file is missing from the location. Actually i think that "SaveAs" function is not working properly.

Boolean fileOK = false;
String path = Server.MapPath("/images/Uploads/employee/images/");
if (!(Directory.Exists(path)))
{
Directory.CreateDirectory(path);
}
lblInformation.Visible = true;
if (fuplEmployeeImage.HasFile)
{
String fileExtension = System.IO.Path.GetExtension(fuplEmployeeImage.FileName).ToLower();
String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
}
}

if (fileOK)
{
if (File.Exists(path + fuplEmployeeImage.FileName))
{
lblInformation.Text = fuplEmployeeImage.FileName + " already Exists.Please Rename the file and try again.";
}
else
{
try
{
fuplEmployeeImage.PostedFile.SaveAs(path + fuplEmployeeImage.FileName);
_prEmployee.Attachment = path + fuplEmployeeImage.FileName;
lblInformation.Text = fuplEmployeeImage.FileName + " File Uploaded!";
}
catch (Exception ex)
{
lblInformation.Text = fuplEmployeeImage.FileName + " File could not be Uploaded.";
}
}
}
else
{
lblInformation.Text = "Cannot accept files of this type.";
}
}

Any help is greatly appreciated.

Thanx

If you're using AJAX with the FileUpload control, it's never going to work. See this thread for more details:

http://forums.asp.net/t/1107788.aspx


Hi

Thanx for your response.

I know this thing that File Upload Control doesn't work inside Update Panel of AJAX.For that purpose i had fire PostBacktrigger for thec control which actually calls FileUpload Control.

So that is not an issue

Bye


Hi,deepakleo2003

I'd like to give you a demo:

<%@. Page Language="C#" %>
<%@. Import Namespace="System.IO" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Button1_Click(object sender, EventArgs e)
{
Boolean fileOK = false;
String path = Server.MapPath("images/");
if (!(Directory.Exists(path)))
{
Directory.CreateDirectory(path);
}
lblInformation.Visible = true;
if (fuplEmployeeImage.HasFile)
{
String fileExtension = System.IO.Path.GetExtension(fuplEmployeeImage.FileName).ToLower();
String[] allowedExtensions = { ".gif", ".jpg", ".png", ".jpeg" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
break;
}
}

if (fileOK)
{
if (File.Exists(path + fuplEmployeeImage.FileName))
{
lblInformation.Text = fuplEmployeeImage.FileName + " already Exists.Please Rename the file and try again.";
}
else
{
try
{
fuplEmployeeImage.PostedFile.SaveAs(path + fuplEmployeeImage.FileName);
//_prEmployee.Attachment = path + fuplEmployeeImage.FileName;
lblInformation.Text = fuplEmployeeImage.FileName + " File Uploaded!";
}
catch
{
lblInformation.Text = fuplEmployeeImage.FileName + " File could not be Uploaded.";
}
}
}
else
{
lblInformation.Text = "Cannot accept files of this type.";
}
}
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblInformation" Visible="false" runat="server" Text="lblInformation"></asp:Label>
<asp:FileUpload ID="fuplEmployeeImage" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
</form>
</body>
</html>

If this help you,don't forget mark it as a answer.Thanks!

Let me know if you need more info.

No comments:

Post a Comment