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

标签 c# asp.net-mvc image file-upload

我想上传一个图像文件到项目的文件夹,但我的捕获有一个错误: 找不到路径“C:\project\uploads\logotipos\11111\”的一部分。

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

我的代码是:

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)
{
}

有人知道我做错了什么吗?

谢谢你:)

最佳答案

试试这个:

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

关于c# - 如何保存上传的文件?由 c#mvc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17944645/

相关文章:

javascript - 如何在运行时在knockoutjs对象中添加新的计算属性?

php - 如何在我的网页中显示Mysql中保存为Medium Blob的图片?

jquery - MVC 发布 Json 错误

图像在 firefox 中呈现的方式与在 chrome 中呈现的不同

objective-c - 将 valuePath 绑定(bind)到 NSImageView 会导致 NSImageView.image 在将新图像拖动到 imagewell 时返回 nil

c# - 尝试解析失败后保留默认值

c# - 如何将类实例转换为 JsonDocument?

c# - MS BotFramework v4 中的对话框将其变量值保留在新对话中

c# - 如何确定异步套接字服务器中的流结束

c# - 在 Web Api Controller 中使用重定向(发现 HTTP 302)