.net MVC中 在视图中上传的附件图片怎么保存到数据库
具体如下视图中有一个上传附件<inputtype="file"/><inputtype="submit"value="确定"/>数据库中有附件表,包括,id,img类型的...
具体如下 视图中 有一个上传附件<input type="file"/><input type="submit" value="确定"/> 数据库中有附件表,包括,id,img类型的列 现在怎么将附件上传的图片,保存到数据库中?求具体代码!在线等
展开
1个回答
展开全部
图片保存到数据库不是最佳的选择.
你可以搜索一下Uploadify 插件. 这个插件非常好用
一般将图片存为图片文件.
大致代码如下:
$("#btn_upload_attachment").uploadify({
height: 25,
swf: '../Scripts/plugin/uplodify/uploadify.swf',
uploader: '/Home/Upload',
queueSizeLimit: 1,
formData: { ID: newId },
buttonText: '选择文件',
width: 80,
onUploadSuccess: function (file, data, response) {
eval("data=" + data);
AddToAttachmentList(data.Data);
}
});
后台代码处理:
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult Upload(HttpPostedFileBase fileData, Guid? ID)
{
if (fileData != null)
{
try
{
// 文件上传后的保存路径
var filePath = Path.Combine(ConfigurationManager.AppSettings["BusinessFiles"], Ticket.OrgId.ToString());
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
var fileName = Path.GetFileName(fileData.FileName);// 原始文件名称
var fileExtension = Path.GetExtension(fileName); // 文件扩展名
var fileID = Guid.NewGuid();
var saveName = fileID.ToString() + fileExtension; // 保存文件名称
fileData.SaveAs(filePath + "/" + saveName);
// 作为临时附件存入附件表
var attachments = new Attachments();
attachments.ID = fileID;
attachments.OrgID = Ticket.OrgId;
attachments.BusinessType = (byte)BusinessType.TransferContract;
attachments.Status = (byte)AttachmentStatus.Temp;
if (ID.HasValue)
{
attachments.BusinessID = ID.Value;
}
attachments.Extension = fileExtension;
attachments.Name = fileName;
attachments.Size = fileData.ContentLength;
attachments.UploadTime = GetNow();
attachments.UploadBy = Ticket.EmployeeName;
attachments.UploadByID = Ticket.UserId;
AttachmentsBLL.SaveAttachment(attachments);
return Json(new { Success = true, FileName = fileName, SaveName = saveName, FileID = fileID, Data = attachments });
}
catch (Exception ex)
{
return Json(new { Success = false, Message = ex.Message }, JsonRequestBehavior.AllowGet);
}
}
else
{
return Json(new { Success = false, Message = "请选择要上传的文件!" }, JsonRequestBehavior.AllowGet);
}
}
你可以搜索一下Uploadify 插件. 这个插件非常好用
一般将图片存为图片文件.
大致代码如下:
$("#btn_upload_attachment").uploadify({
height: 25,
swf: '../Scripts/plugin/uplodify/uploadify.swf',
uploader: '/Home/Upload',
queueSizeLimit: 1,
formData: { ID: newId },
buttonText: '选择文件',
width: 80,
onUploadSuccess: function (file, data, response) {
eval("data=" + data);
AddToAttachmentList(data.Data);
}
});
后台代码处理:
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult Upload(HttpPostedFileBase fileData, Guid? ID)
{
if (fileData != null)
{
try
{
// 文件上传后的保存路径
var filePath = Path.Combine(ConfigurationManager.AppSettings["BusinessFiles"], Ticket.OrgId.ToString());
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
var fileName = Path.GetFileName(fileData.FileName);// 原始文件名称
var fileExtension = Path.GetExtension(fileName); // 文件扩展名
var fileID = Guid.NewGuid();
var saveName = fileID.ToString() + fileExtension; // 保存文件名称
fileData.SaveAs(filePath + "/" + saveName);
// 作为临时附件存入附件表
var attachments = new Attachments();
attachments.ID = fileID;
attachments.OrgID = Ticket.OrgId;
attachments.BusinessType = (byte)BusinessType.TransferContract;
attachments.Status = (byte)AttachmentStatus.Temp;
if (ID.HasValue)
{
attachments.BusinessID = ID.Value;
}
attachments.Extension = fileExtension;
attachments.Name = fileName;
attachments.Size = fileData.ContentLength;
attachments.UploadTime = GetNow();
attachments.UploadBy = Ticket.EmployeeName;
attachments.UploadByID = Ticket.UserId;
AttachmentsBLL.SaveAttachment(attachments);
return Json(new { Success = true, FileName = fileName, SaveName = saveName, FileID = fileID, Data = attachments });
}
catch (Exception ex)
{
return Json(new { Success = false, Message = ex.Message }, JsonRequestBehavior.AllowGet);
}
}
else
{
return Json(new { Success = false, Message = "请选择要上传的文件!" }, JsonRequestBehavior.AllowGet);
}
}
追问
我想知道,通过这个控件,怎么样接收并封装成指定实例的对象,对象只有两列。id(自增长),adimg 也就是说,我只需要将前台图片传到后台 然后直接插入数据库就行了 中间可以用二进制数组转换 但是我不知道怎么接收 万望解答 谢谢!
追答
数据库存二进制我觉得不好, 你只需要存成文件, 给文件指定一个GUID, 数据库记录这个GUID就可以了
var saveName = fileID.ToString() + fileExtension; // 保存文件名称
fileData.SaveAs(filePath + "/" + saveName);
这就是保存成文件的代码
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询