【发布时间】:2018-08-28 22:40:23
【问题描述】:
我在 Property Project 工作,允许系统用户为它添加属性上传图像。我需要做什么创建文件夹取决于属性的ID,将路径保存在数据库中并将图像上传到此文件夹中。
我在我的属性模型中创建:
public string Images { get; set; }
在我创建的视图中:
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-6">
<div class="form-group">
<label class="control-label"> choose image </label>
<input multiple type="file" title="choose image" id="files" name="PropImage" onchange="show(this)" />
</div>
</div>
<div class="col-md-4"></div>
</div>
我的 JavaScript :
<script type="text/javascript">
function handleFileSelect() {
//Check File API support
if (window.File && window.FileList && window.FileReader) {
var files = event.target.files; //FileList object
var output = document.getElementById("result");
for (var i = 0; i < files.length; i++) {
var file = files[i];
//Only pics
if (!file.type.match('image')) continue;
var picReader = new FileReader();
picReader.addEventListener("load", function (event) {
var picFile = event.target;
var div = document.createElement("div");
div.innerHTML = "<img class='thumbnail' src='" + picFile.result + "'" + "title='" + picFile.name + "'/>";
output.insertBefore(div, null);
});
//Read the image
picReader.readAsDataURL(file);
}
} else {
console.log("Your browser does not support File API");
}
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
现在在我的控制器中我无法创建文件夹,因为尚未创建属性 ID,我该怎么办?我应该有权创建文件夹吗?
我的控制器:
public ActionResult CreateProperties(AddPropertyViewModel 模型, 列出道具图像) {
if (ModelState.IsValid)
{
foreach (var item in PropImage)
{
var path = Path.Combine(Server.MapPath(@"~\ImgUp\" /*+ PropImagefolderName*/), item.FileName);//+ IdentityImageFolderName)
item.SaveAs(path);
}
//model.PropertiesVM.ID = properToAdd.PropAddress.Id;
// string name = Path.GetFileNameWithoutExtension(fileName); //getting file name without extension
// string myfile = name + "_" + properToAdd.ID + ext; //appending the name with id
// store the file inside ~/project folder(Img)
var imagepath = Server.MapPath(PropImageDirctory);
//var IdentityPath = Server.MapPath(IdentityImageDirctory);
properToAdd.Images = imagepath;
properToAdd.Build_area = model.PropertiesVM.Build_area;
properToAdd.Earth_area = model.PropertiesVM.Earth_area;
properToAdd.AddedDate = DateTime.Now;
properToAdd.Prop_Type_ID = Prop_type_ID;
properToAdd.Plate_ID = plateID;
properToAdd.Branch_ID = BranshID;
properToAdd.Price = model.PropertiesVM.Price;
properToAdd.AddedBy = FullName; /*currentUserName;*/
db.D_Properties.Add(properToAdd);
//IdentityImage.SaveAs(Path.Combine(IdentityFolderPath, IdentityImageFileName));
// InstrumentImage.SaveAs(Path.Combine(IdentityPath, InstrumentImageFileName));
db.SaveChanges();
TypesDropDownList();
PlatesDropDownList();
BranchesDropDownList();
TempData["noti"] = "Success";
return RedirectToAction("CreateProperties");
}
//ViewBag.message = "Please choose only Image file";
// If we got this far, something failed, redisplay form
TypesDropDownList();
PlatesDropDownList();
BranchesDropDownList();
TempData["noti"] = "Error";
return View();
}
【问题讨论】:
-
属性id是谁生成的?数据库是我的猜测。您需要将其写入数据库以检索 id - 这不是问题,因为您不需要进一步的信息来“创建”该属性。检索生成的 id 可以在与数据库插入相同的操作中完成 - 无论您是否使用实体框架。请在您的帖子中添加更多详细信息。
-
是 id 从数据库生成。你的意思是在保存更改方法后我可以获得当前的 id 吗?
-
是的,你可以。在不检索 id 的情况下编写您的控制器操作,编辑您的帖子并添加控制器的定义,然后我们将向您展示如何检索 id。
-
我更新了我的问题
标签: c# asp.net file model-view-controller file-upload