[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 500
  • Last Modified:

AsyncFileUpload in three tier architecture

Hi Experts,
I am using AsyncFileUpload.My project has three tier architecture.
I am storing image name in database and storing image in folder on the server.
I am following 3 tier architecture.

In my code
MyEmployee oEmployee = new MyEmployee();
 if (AsyncFileUpload1.HasFile)
                {
                    oEmployee.ImageName = Convert.ToString(AsyncFileUpload1.FileName);
                }



In Employee.cs
  public string ImageName
          {
              get
              {
                  return _imageName;
              }
              set
              {
                  _imageName = value;
              }
          }


Then in datalayer I am accessing this property

 sqlParams[41] = DBDataHandler.GetSqlParameter("@Photo", objEmployee.ImageName, SqlDbType.VarChar);



But I am confused,how should I upload the image in the folder on server?
How do I follow this technique.
Your help is really appreciated.
Thanks in advance.
0
Johny Bravo
Asked:
Johny Bravo
  • 2
  • 2
2 Solutions
 
Ivo StoykovCommented:
try something like

HTH

Ivo Stoykov

string filename  = System.IO.Path.GetFileName(AsyncFileUpload1.FileName);
AsyncFileUpload1.SaveAs(Server.MapPath("Uploads/") + filename); 
if(!String.IsNullOrEmpty(filename)) {   oEmployee.ImageName = filename; }

Open in new window

0
 
Johny BravoAuthor Commented:
Hmm ok so I don't need to go to DataLayer for storing images. Right?
One more thing,I am changing file name of the uploaded file.
strUploadFolder = Request.PhysicalApplicationPath + "../Images/EmployeePics\\";
                    strExtension = Path.GetExtension(AsyncFileUpload1.PostedFile.FileName);
                    AsyncFileUpload1.SaveAs(strUploadFolder + "Test" + extension);

Here instead of "Test" ,I want to add name of the username which he enters in the textbox.
txtFirstName.Text + txtLastName.Text.

How to do this?
0
 
masterpassCommented:
you don't have to do that in DataLayer .. can do it in the UI Layer itself ...

Here instead of "Test" ,I want to add name of the username which he enters in the textbox.
txtFirstName.Text + txtLastName.Text.


string FileNameToSave = txtFirstName.Text + txtLastName.Text+extension;
AsyncFileUpload1.SaveAs(Server.MapPath(System.IO.Path.Combine(strUploadFolder , FileNameToSave)));

Open in new window

0
 
masterpassCommented:
forgot to update the first line

Here is the updated code
strUploadFolder = "Images\EmployeePics";\\When you are giving like this the Image folder should be in the root of the project, If it's not then correctlt specify the path here 
strExtension = Path.GetExtension(AsyncFileUpload1.PostedFile.FileName);
string FileNameToSave = txtFirstName.Text + txtLastName.Text + extension;
AsyncFileUpload1.SaveAs(Server.MapPath(Path.Combine(strUploadFolder , FileNameToSave)));

Open in new window

0
 
Johny BravoAuthor Commented:
As both solutions helped me so I am distributing points.
Thanks a lot
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now