上传的文件不保存到文件系统 [英] Uploaded file not saving to file system
本文介绍了上传的文件不保存到文件系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建为我建立一个CMS媒体库。基本功能包括上传文件并将其存储在一个文件系统中。但是,它会为保存的文件目前,我通过什么我已经通过本地主机
内置测试的ID。
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(媒体文件);
添加文件的名称,但是,它不会将图像添加到目录
db.Files.Add(mediafile);
adds the name of the file, however, it does not add an image to the directory.
我如何发布文件保存到一个目录? (注释部分)
How do I save posted files to a directory? (The Commented Section)
[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; }
}
}
使用的资源
推荐答案
使用的HttpPostedFile对象的SaveAs方法:
Use the SaveAs method of the HttpPostedFile object:
upload.SaveAs("Path\\to\\image\\directory");
这篇关于上传的文件不保存到文件系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文