5

I want upload an image file to project's folder but I have an error in my catch: Could not find a part of the path 'C:\project\uploads\logotipos\11111\'.

What am I do wrong? I want save that image uploaded by my client in that folder... that folder exists... ah if I put a breakpoint for folder_exists3 that shows me a true value!

My code is:

try
{
    var fileName = dados.cod_cliente;
    bool folder_exists = Directory.Exists(Server.MapPath("~/uploads"));
    if(!folder_exists)
        Directory.CreateDirectory(Server.MapPath("~/uploads"));
    bool folder_exists2 = Directory.Exists(Server.MapPath("~/uploads/logo"));
    if(!folder_exists2)
        Directory.CreateDirectory(Server.MapPath("~/uploads/logo"));
    bool folder_exists3 = Directory.Exists(Server.MapPath("~/uploads/logo/" + fileName));
    if(!folder_exists3)
        Directory.CreateDirectory(Server.MapPath("~/uploads/logo/"+fileName));

    file.SaveAs(Server.MapPath("~/uploads/logo/" + fileName+"/"));
}
catch(Exception e)
{
}

Someone knows what I'm do wrong?

Thank you :)

6
  • But you are not checking if \uploads\logotipos\11111\ exists just for uploads/logo/ Commented Jul 30, 2013 at 10:34
  • I know that folder exists... in this step I just want save file in that specific folder, when this step already done I will create that verification Commented Jul 30, 2013 at 10:38
  • 1
    file.SaveAs(Server.MapPath("~/uploads/logo/" + fileName)); Commented Jul 30, 2013 at 10:38
  • what is the content of fileName and how should your full save filename should be? Commented Jul 30, 2013 at 10:45
  • I already test this and the result is the same! Commented Jul 30, 2013 at 10:46

4 Answers 4

24

Try this:

string targetFolder = HttpContext.Current.Server.MapPath("~/uploads/logo");
string targetPath = Path.Combine(targetFolder, yourFileName);
file.SaveAs(targetPath);
Sign up to request clarification or add additional context in comments.

Comments

1

Your error is the following:

bool folder_exists3 = Directory.Exists(Server.MapPath("~/uploads/logo/" + fileName));
if(!folder_exists3)
    Directory.CreateDirectory(Server.MapPath("~/uploads/logo/"+fileName));

You check if a directory exists, but you should check if the file exists:

File.Exists(....);

3 Comments

but I don't want verify if file exists but yes if folder exists
then delete that statement. You check the folder with folder_exists2
I need this step for the future upload...and the program don't need create again the same folder
1

You need filename

file.SaveAs(Server.MapPath("~/uploads/logo/" + fileName+"/" + your_image_fillename));

Comments

0

Remove the last part of the path to save you have an extra "/"

It should be

file.SaveAs(Server.MapPath("~/uploads/logo/" + fileName);

Also you do not have a file extension set.

6 Comments

unfortunately this don't resolve my problem, I already test this and give to me the same error! :/
C:\project\uploads\logotipos\11111\' thats your error message right? Well its different than your save as you are looking for \logo not logotipos. also i forgot a bracket - file.SaveAs(Server.MapPath("~/uploads/logo/") + fileName);
I try change to your proposal but don't work :/ file.SaveAs(Server.MapPath("~/uploads/logo/") + fileName); Give to me the same error!
I tried: file.SaveAs(@"C:\" + fileName) and I could copy... and my answer now is: "Why I can't copy to that specific folder?"
permissions? What user is your process running as and does that user have read/write persmission on that specific folder?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.