HttpPostedFileBase.SaveAs 工作但没有上传文件,也没有异常

发布于 2024-12-07 20:18:19 字数 857 浏览 0 评论 0原文

首先,这是我的代码:

private Shoe ProcessForm(Shoe shoe, HttpPostedFileBase image)
{
    try
    {
        shoe.Slug = CMSHelper.SanitizeTitle(shoe.Name);
        shoe.LastModification = DateTime.Now;

        if ((image != null) && (image.ContentLength > 0))
        {
            string fileName = String.Concat(shoe.ShoeId, Path.GetExtension(image.FileName));
            shoe.Image = fileName;

            string filePath = Path.Combine(Server.MapPath(shoe.ImagePath), fileName);
            image.SaveAs(filePath);
        }
    }
    catch (Exception e)
    {
        throw e;
    }

    return shoe;
}

在本地,此代码运行良好。目录的权限很好。而且它之前曾在其他服务器上随机运行过(当我测试 VPS 提供商时,我在 4 或 5 个不同的服务器上测试了此代码)。

但是,如果我尝试从家用计算机运行它,一切都会顺利,数据库中保存了一个文件名,但没有上传文件。并且没有例外!!!

我已经尝试解决这个问题近三天了,花了很多无用的时间,请帮助我......我只是不明白这有什么问题......

First, here is my code:

private Shoe ProcessForm(Shoe shoe, HttpPostedFileBase image)
{
    try
    {
        shoe.Slug = CMSHelper.SanitizeTitle(shoe.Name);
        shoe.LastModification = DateTime.Now;

        if ((image != null) && (image.ContentLength > 0))
        {
            string fileName = String.Concat(shoe.ShoeId, Path.GetExtension(image.FileName));
            shoe.Image = fileName;

            string filePath = Path.Combine(Server.MapPath(shoe.ImagePath), fileName);
            image.SaveAs(filePath);
        }
    }
    catch (Exception e)
    {
        throw e;
    }

    return shoe;
}

Locally, this code works fine. Directories' permissions are fine. And it has worked before randomly on other servers (I tested this code on 4 or 5 different servers while I was testing VPS providers).

But if I try to run it from my home computer, everything passes alright, there's a file name saved in the database but no file is uploaded. And no exceptions are given!!!

I've been trying to fix this for almost three days and so much useless hours, please help me... I just don't see what's wrong with this...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

烟酉 2024-12-14 20:18:20

你如何设置shoe.ImagePath?您的问题感觉像是 Server.MapPath 返回了您不期望的值。

How are you setting shoe.ImagePath? Your issue feels like Server.MapPath is returning a value that you are not expecting.

何其悲哀 2024-12-14 20:18:20

您是否检查过您是否有多个网络服务器?也许像负载均衡器。它可能不会抛出异常,因为实际上没有异常,并且文件确实存在于某个地方,只是不在您认为它所在的服务器上。只是一个想法(我以前也遇到过这种情况)。

Did you check to see if you have multiple web servers? Like a load balancer perhaps. It might not be throwing an exception because there isn't actually an exception and the file does actually exist somewhere, just not on the server you believe it is on. Just a thought (it has happened to me before).

凉城 2024-12-14 20:18:20

确保您使用 enctype="multipart/form-data" 格式的

http://www.prideparrot.com/blog/archive/2012/8/uploading_and_returning_files

Make sure you are using enctype="multipart/form-data" in the form

http://www.prideparrot.com/blog/archive/2012/8/uploading_and_returning_files

你列表最软的妹 2024-12-14 20:18:20

我也遇到了在此线程上发布的相同问题,在尝试上传文件几次后,我刷新了文件所在的文件夹,最后我在那里看到了它们。不知道为什么它在第一次刷新后第一次没有显示,但可能与刷新文件夹有关。我在 Visual Studio 中进行了刷新,并且还打开了“显示所有文件”按钮。

I also ran into the same issue posted on this thread and after a few tries of uploading a file, I refreshed the folder where the files were suppose to be and finally I saw them there. Not sure why it didn't show up the first time after the first refresh but it probably has something to do with refreshing the folder. I did the refresh from within Visual Studio and I also had the "Show All Files" button on.

装迷糊 2024-12-14 20:18:19

我最终做了一个解决方法,效果非常好。我什至问过我的工作,每个人都说没有任何问题。所以搞砸了,这就是我所做的:

我没有调用 .SaveAs(),而是创建了一个方法:

public static void WriteFileFromStream(Stream stream, string toFile)
{
    using (FileStream fileToSave = new FileStream(toFile, FileMode.Create))
    {
        stream.CopyTo(fileToSave);
    }
}

我这样称呼它:

CMSHelper.WriteFileFromStream(image.InputStream, filePath);

就是这样。

I finally did a workaround which is doing very fine. I even asked at my job and everyone said there was nothing wrong at all. So screw it here's what I did:

Instead of calling .SaveAs() I made a method which is :

public static void WriteFileFromStream(Stream stream, string toFile)
{
    using (FileStream fileToSave = new FileStream(toFile, FileMode.Create))
    {
        stream.CopyTo(fileToSave);
    }
}

I call it like this:

CMSHelper.WriteFileFromStream(image.InputStream, filePath);

And that's it.

痴梦一场 2024-12-14 20:18:19

你的视图模型是什么?首先将 (Shoehoe, HttpPostedFileBase image) 替换为 (FormCollection formCollection)。在其上放置一个断点以查看您是否获得了所有提交的值。如果您这样做,则模型绑定存在问题,我们需要解决该问题。

编辑

您能否在

image.SaveAs(filePath); 

“添加文件名”和“文件路径”变量的监视上放置一个断点。我认为服务器路径不是您期望的那样,或者您可能正在一起查找错误的文件夹。告诉我们这些变量的值是多少。还要确保当您经过 image.SaveAs(filePath); 时不会引发异常

What is your view model? First of all replace (Shoe shoe, HttpPostedFileBase image) with (FormCollection formCollection). Put a breakpoint on that to see that you are getting all the submitted values. If you do then there is a problem with model binding and we'll need to address that.

Edit

Can you please put a breakpoint on

image.SaveAs(filePath); 

Add a watch for fileName and filePath variables. I think that server path is not what you expect it to be, or maybe you are looking in the wrong folder all together. Tell us what are the values of those variables. Also make sure that no exception is thrown when you go past image.SaveAs(filePath);

猛虎独行 2024-12-14 20:18:19

image.FileName 包含路径数据,并通过

Path.Combine(Server.MapPath(shoe.ImagePath), fileName)

尝试如下选择:

string pathForSaving = Server.MapPath("~/Upload/Images");
string uploadFileName = System.IO.Path.GetFileName(image.FileName);
string uploadFilePathAndName = Path.Combine(pathForSaving, uploadFileName);
image.SaveAs(uploadFilePathAndName);

image.FileName contains the path data and is selected by

Path.Combine(Server.MapPath(shoe.ImagePath), fileName)

Try something like:

string pathForSaving = Server.MapPath("~/Upload/Images");
string uploadFileName = System.IO.Path.GetFileName(image.FileName);
string uploadFilePathAndName = Path.Combine(pathForSaving, uploadFileName);
image.SaveAs(uploadFilePathAndName);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文