.NET Upload to path

.NET Upload to path

pcjacksonpcjackson Posts: 18Questions: 6Answers: 0

I'm trying to upload to a specific path, but only store the filename in the database (in the same table as the rest of the data, not in a separate table. I am porting an existing application.)

Here is what I am trying:

                    .Upload(new Upload((file, id) => {
                            file.SaveAs(request.PhysicalApplicationPath + @"App_Data\Images\" + Upload.DbType.FileName);
                            return Upload.DbType.FileName;
                        })

But I do not fully understand the way Action works. Can someone explain it to me?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,195Questions: 1Answers: 10,412 Site admin
    Answer ✓

    Upload.DbType.FileName is a constant that the Upload().Db() method understands. The Action doesn't do anything with those constants.

    The return from the Action is basically the value that the field will take (i.e. the value that references the file - in this case the file name).

    To get the file name you need to use the first parameter passed into the action function which is an HttpPostedFile. So file.FileName should give you the file name.

    I would urge caution with this approach of uniquely identifying files by file name though. Consider two different files being uploaded with the same file name. Or the same file with different revisions. The new upload would override the old one!

    For that reason using a database id as a unique value is a good idea. Or use a hash (but make sure it doesn't clash and you have access to the file's meta information such as the file name, so it makes sense to the end user).

    Allan

  • pcjacksonpcjackson Posts: 18Questions: 6Answers: 0
    edited November 2019

    Thanks! That seems obvious in retrospect. >_>

    I know all of the drawbacks. And frankly, I wouldn't have written it this way. I am simply porting existing code someone else wrote, and keeping the spirit of doing things in stages so they actually can get done.

This discussion has been closed.