如何保存上传的文件? c#mvc

时间:2022-09-02 15:25:48

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\'.

我想将一个图像文件上传到项目的文件夹,但是我的catch中有一个错误:无法找到路径'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!

我做错了什么?我希望保存我的客户端在该文件夹中上传的图像...该文件夹存在...啊,如果我为folder_exists3设置一个断点,显示一个真正的值!

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 :)

谢谢 :)

4 个解决方案

#1


19  

Try this:

string targetFolder = HttpContext.Current.Server.MapPath("~/uploads/logo");
string targetPath = Path.Combine(targetFolder, yourFileName);
file.SaveAs(targetPath);

#2


0  

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


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.

您也没有文件扩展名集。

#4


0  

You need filename

你需要文件名

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

#1


19  

Try this:

string targetFolder = HttpContext.Current.Server.MapPath("~/uploads/logo");
string targetPath = Path.Combine(targetFolder, yourFileName);
file.SaveAs(targetPath);

#2


0  

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


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.

您也没有文件扩展名集。

#4


0  

You need filename

你需要文件名

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