我在Property Project中工作,允许系统用户为其添加属性上传图像。我需要做什么创建文件夹取决于属性的ID,保存数据库中的路径并将图像上传到此文件夹中。
我在我的Property模型中创建:
public string Images { get; set; }
在View i中创建:
<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 model, 列出PropImage) {
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();
}
答案 0 :(得分:1)
如果您的Property
类(或任何类型properToAdd
)具有正确定义的id
属性,则在您调用db.SaveChanges();
对象properToAdd
后&#39; id将包含新的数据库记录ID - 问题已解决。
答案 1 :(得分:1)
如果要通过ajax
或其他任何方式保存图像,然后再保存属性详细信息,这意味着您仍未为新记录分配任何ID,那么在我看来,您可以按照以下步骤进行操作。
tempImgs
文件夹。将文件重命名为$ _SESSION ['userid'] + i '../uploads/'+<<new record id>>
的目录tempImgs
移动到新创建的文件夹希望这能解决您的问题