C# 根据ID创建文件夹,上载图像,并在asp.net MVC的数据库中保存路径
我在属性项目中工作,该项目允许系统用户在为其添加属性上载图像时使用。我需要做什么创建文件夹取决于属性的ID,保存在数据库中的路径,并上传在这个文件夹中的图像 我在我的属性模型中创建:C# 根据ID创建文件夹,上载图像,并在asp.net MVC的数据库中保存路径,c#,asp.net,file,model-view-controller,file-upload,C#,Asp.net,File,Model View Controller,File Upload,我在属性项目中工作,该项目允许系统用户在为其添加属性上载图像时使用。我需要做什么创建文件夹取决于属性的ID,保存在数据库中的路径,并上传在这个文件夹中的图像 我在我的属性模型中创建: public string Images { get; set; } 鉴于此,我创建了: <div class="row"> <div class="col-md-2"></div>
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>
如果您的
属性
类(或properToAdd
的任何类型)具有正确定义的id
属性,一旦调用db.SaveChanges()
对象properToAdd
的id将包含新的数据库记录id-问题已解决。如果调用db.SaveChanges(),您的属性
类(或properToAdd
的任何类型)具有正确定义的id
属性
对象properToAdd
的id将包含新的数据库记录id-问题已解决。如果要通过ajax
或其他方式上载图像,则在保存属性详细信息之前,这意味着您仍然没有为新记录分配任何id,那么在我看来,您可以执行以下操作
tempImgs
文件夹。将文件重命名为$\u会话['userid']+i'../uploads/'+
tempImgs
移动到新创建的文件夹希望这能回答您的问题如果您打算通过
ajax
或其他方式上传图像,在保存属性详细信息之前,这意味着您仍然没有为新记录分配任何id,那么在我看来,您可以执行以下操作
tempImgs
文件夹。将文件重命名为$\u会话['userid']+i'../uploads/'+
tempImgs
移动到新创建的文件夹希望这能回答您的问题谁生成了属性id?数据库是我的猜测。您需要将其写入数据库以检索id—这不是问题,因为您不需要进一步的信息来“创建”属性。检索生成的id可以在与数据库插入相同的操作中完成—无论您是否使用实体框架。请在您的帖子中添加更多详细信息。是的id从数据库生成。你是说在保存更改方法后我可以获得当前id?是的,你可以。编写控制器操作而不检索id,编辑帖子并添加控制器的定义,然后我们将向您展示如何检索id。我更新了我的问题谁生成属性id?数据库是我的猜测。您需要将其写入数据库以检索id—这不是问题,因为您不需要进一步的信息来“创建”属性。检索生成的id可以在与数据库插入相同的操作中完成—无论您是否使用实体框架。请在您的帖子中添加更多详细信息。是的id从数据库生成。你是说在保存更改方法后我可以获得当前id?是的,你可以。编写控制器操作而不检索id,编辑帖子并添加控制器的定义,然后我们将向您展示如何检索id。我更新了我的问题
<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);
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();
}