System.IO.File.Exists(fpath)在Chrome,火狐返回false [英] System.IO.File.Exists(fpath) returns false in Chrome,Firefox

查看:92
本文介绍了System.IO.File.Exists(fpath)在Chrome,火狐返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code这在IE浏览器,但不能在Firefox和谷歌Chrome工作。

I have the following code which works in IE but not in Firefox and Google Chrome.

public ActionResult LogoForm(HttpPostedFileBase file)
        {
            if (file != null)
            {
                string fpath = file.FileName;
                if (System.IO.File.Exists(fpath))
                { 
                   //logic comes here
                }
        }

在我看来,我有这样的:

In my View I have this:

@using (Html.BeginForm("LogoForm", "LogoEditor", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <text>Logo Image &nbsp;&nbsp;&nbsp;</text>
    <input type="file" name="file" id="file" /> <text> &nbsp; &nbsp; &nbsp;</text>
    <input type="submit" name="upload" value="Upload" />
}

在Firfox和Chrome,该行的任何文件的情况下,如果(System.IO.File.Exists(fpath))总是返回false!..它没有找到该文件。为什么会这样?

In case of any file in Firfox and Chrome, the line 'if (System.IO.File.Exists(fpath))' always returns false!..It doesn't find the file. Why so??

推荐答案

file.FileName 包含文件路径的客户端电脑,而不是在服务器上。你不应该使用它在服务器上。这部作品在IE浏览器的原因是因为IE发生的完整路径发送到文件服务器,因为你正在运行在客户端和它的作品在同一台机器上的服务器。 Chrome和FF出于安全原因,从不发送的文件路径。 IIRC它们发送虚设路径到不存在任何地方的服务器。这不会与IE浏览器既不当您部署在IIS应用程序和远程访问它。

file.FileName contains the file path on the client computer, not on the server. You should not use it on the server. The reason this works in IE is because IE happens to send the full path to the file to the server and since you are running both the client and the server on the same machine it works. Chrome and FF for security reasons never send the file path. IIRC they send a dummy path to the server that doesn't exist anywhere. This won't work with IE neither when you deploy your application in IIS and access it remotely.

您永远不应该依赖于 file.FileName 的路径部分。您应该只提取文件名,然后用一些路径串联它的服务器

You should never rely on the path portion of file.FileName. You should only extract the filename and then concatenate it with some path on the server:

例如像

[HttpPost]
public ActionResult LogoForm(HttpPostedFileBase file)
{
    if (file != null)
    {
        string path = Path.GetFileName(file.FileName);
        string fileName = Path.Combine(Server.MapPath("~/App_Data"), path);
        if (File.Exists(fileName))
        { 
            // logic comes here
        }
    }
}

我也建议你检查出以下博客帖子关于ASP.NET MVC上传文件。

I would also recommend you checking out the following blog post about uploading files in ASP.NET MVC.

这篇关于System.IO.File.Exists(fpath)在Chrome,火狐返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆