简体   繁体   中英

Uploaded file not saving to file system

Context

I am creating a media library for a CMS that I am building. The basic function includes uploading a file and storing it in a file system. However, it creates an id for the saved file Currently I am testing through what I have built via localhost .

db.Files.Add(mediafile); adds the name of the file, however, it does not add an image to the directory.


Question

How do I save posted files to a directory? (The Commented Section)


Source Code

/Areas/Media/Controllers/FilesController.cs

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "FileId,FileName,FileURL")] File file, HttpPostedFileBase upload)
    {
        if (ModelState.IsValid)
        {
            if (upload != null && upload.ContentLength > 0)
            {
                var mediafile = new File
                {
                    FileName = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(upload.FileName),
                    FileType = FileType.File
                };
                //As seen in tutorial
                //instructor.FilePaths = new List<FilePath>();
                //instructor.FilePaths.Add(photo);

                //Educated Guessing
                //db.Files = new List<File>();
                //db.Files.Add(mediafile);
            }
            db.Files.Add(file);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(file);
    }

/Areas/Media/Views/File/Create.cshtml

@using (Html.BeginForm("Create", "Files", FormMethod.Post, new { enctype = "multipart/Form-data" }))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">

        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            <div class="control-label col-md-2">
                <label for="file">Upload Image  for Slide:</label>
            </div>
            <div class="col-md-10">
                <input type="file" name="upload" id="fileId" style="width:50%" />
            </div>

        </div>



        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

/Models/Media.cs

using System.Web;

namespace JosephMCasey.Models
{
    public enum FileType
    {
        Thumb = 1, File
    }
    public class File
    {
        [Key]
        public int FileId { get; set; }
        [StringLength(255)]
        public string FileName { get; set; }
        public FileType FileType { get; set; }
    }
}

Used Resource

Mike Dot Netting

My Github Repo

使用HttpPostedFile对象的SaveAs方法:

upload.SaveAs("Path\\to\\image\\directory");

Try accepting a

HttpPostedFileWrapper 

instead of

HttpPostedFileBase 

HttpPostedFileBase is an abstract class

You also need to actually call

upload.SaveAs(fileName) 

As your db appears to be saving the entry in a database but not to your filesystem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM