HttpPostedFileBase.SaveAs working but no file uploaded and no exceptions
Asked Answered
W

7

6

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

Wholesome answered 5/10, 2011 at 3:41 Comment(6)
Please keep things like "C#/ASP.NET MVC 3: " out of your titles. Those are tags, and belong in the tags, not in your title.Subsolar
Also, don't use try {} catch (Exception e){throw e;}. If you're going to do that, then you're better off without the try/catch block at all. Your code will trash the stack trace.Subsolar
Alright for the first comment, I won't do it again. As for your second comment : What would you do? Ah I get what you mean, the exception will throw itself so it does not require me to catch it and throw it myself, right? Thanks for the comments.Wholesome
exceptions propagate themselves without our help. Also, your code will make it look like the exception came from the location of the throw.Subsolar
Maybe a stupid question, but are you sure the form tag ahve the enctype? Html.BeginForm("ProcessForm", "Home", null, FormMethod.Post, new { enctype = "multipart/form-data" })Boxboard
Yes: @using (Html.BeginForm("Edit", "Shoes", FormMethod.Post, new { enctype = "multipart/form-data" }))Wholesome
W
5

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.

Wholesome answered 19/10, 2011 at 4:20 Comment(0)
C
5

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

Colloid answered 5/10, 2011 at 8:57 Comment(6)
I receive everything. The thing I find very weird is that when I test it LOCALLY everything works alright. I then, deploy it on the server and test it from the server (again locally) and it WORKS. But when I test it from home, on the server (remotely) there's a filename saved in the database but the file isn't saved on the server's harddrive...Wholesome
Have you checked event viewer on the server to see whether there are any access denied exceptions thrown when you try to write the file? For me it sounds like permission issue. If you don't have an event viewer, then try catching and logging that exception in a text file.Colloid
I thought so at first and then I made the folder accessible to Everyone and never worked even though. I'll retry and check out the event viewer, Thanks.Wholesome
As I thought, NOTHING at all in the logs :-/ Any other ideas?Wholesome
Does a folder that you are saving to exists on a server?Colloid
Yes it does. There is no error related to folders or permissions at all... :-SWholesome
W
5

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.

Wholesome answered 19/10, 2011 at 4:20 Comment(0)
S
2

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);
Scapegrace answered 31/12, 2013 at 6:47 Comment(0)
D
0

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

Disharmony answered 17/10, 2011 at 13:25 Comment(1)
it is a simple property set in my model like this: ImagePath = "/Assets/Uploads/Shoes"; And I tested the Server.MapPath and it returns a perfect existing path ... :-SWholesome
B
0

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

Boarding answered 24/10, 2011 at 3:45 Comment(0)
F
0

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

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

Fiedler answered 11/7, 2013 at 21:22 Comment(0)
W
0

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.

Waldemar answered 29/3, 2016 at 17:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.