图像不保存到SQL和文件夹 [英] Image Not Save to SQL and Folder

查看:95
本文介绍了图像不保存到SQL和文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MVC的初学者,我开发了Image Uploader,它真的很有用,但那个图像没有保存我的上传文件夹和数据库,请帮帮我。

型号



命名空间eData

{

公共课Clr

{



[Key]

public int ClrID {get;组; }

[StringLength(5,ErrorMessage =必须是5个字符,MinimumLength = 5)]

[必需(ErrorMessage =必需)]



public string ClrCode {get;组; }





[StringLength(25,ErrorMessage =必须小于25个字符,MinimumLength = 1)]

[必需(ErrorMessage =必需)]

公共字符串ClrName {get;组; }





public string备注{get;组; }



public bool StatusId {get;组; }





public DateTime StatusChangeDate {get;组; } $ / $


public int CreateBy {get;组; }



public DateTime CreatedDate {get;组; }



public int EditBy {get;组; } b / b
public DateTime EditDate {get;组; }



public string Name {get;组; }

public int Length {get;组; }

公共字符串类型{get;组; }

}

}

Clr控制器(创建部件)



public class ClrController:Controller

{

// GET:Masters / Clr

// public ActionResult Index()

// {

//返回查看();

//}



public FilePathResult Image()

{

string filename = Request.Url.AbsolutePath.Replace(/ Clr / image,);

string contentType =;

var filePath = new FileInfo(Server.MapPath(〜/ Uploaded)+ filename);



var index = filename.LastIndexOf(。)+ 1;

var extension = filename.Substring(index).ToUpperInvariant();



//修复IE无法处理jpg图像类型

contentType = string.Compare(扩展名,JPG)== 0? image / jpeg:string.Format(image / {0},extension);



返回文件(filePath.FullName,contentType);

}



[HttpPost]

public ContentResult UploadFiles()

{

var r = new List< uploadfilesresult>();



foreach(Request.Files中的字符串文件)

{

HttpPostedFileBase hpf = Request.Files [file] as HttpPostedFileBase;

if(hpf.ContentLength == 0)

continue;



string savedFileName = Path.Combine(Server.MapPath(〜/ Uploaded),Path.GetFileName(hpf.FileName));

hpf .SaveAs(savedFileName);



r.Add(new UploadFilesResult()

{

Name = hpf .FileName,

长度= hpf.ContentLength,

类型= hpf.ContentType

});

}

返回内容( {\name \:\+ r [0] .Name +\,\type \:\+ r [0] .Type +\ ,\size \:\+ string.Format({0} bytes,r [0] .Length)+\},application / json);

}





公共ActionResult保存(Clr clr)

{

var objContext = new KktdbContext();

clr.CreateBy = 1;

clr.StatusChangeDate = System.DateTime.Now;

clr.CreatedDate = System.DateTime.Now;

clr.EditBy = 1;

clr.EditDate = System.DateTime.Now;

objContext.Clrs.Add(clr);

objContext.SaveChanges();

TempData [Success] =成功保存;
返回RedirectToAction(ClrIndex,新{A =New});





}

查看-Create.chtml



@using(@ Html.BeginForm(Save,Clr ))

{

Im beginner for the MVC , Im developed the Image Uploader, its really working, but thats image not save the My Upload Folder and Database, please help me.
Model

namespace eData
{
public class Clr
{

[Key]
public int ClrID { get; set; }
[StringLength(5, ErrorMessage = "Must be 5 characters", MinimumLength = 5)]
[Required(ErrorMessage = "Required")]

public string ClrCode { get; set; }


[StringLength(25, ErrorMessage = "Must be less than 25 charcters", MinimumLength = 1)]
[Required(ErrorMessage = "Required")]
public string ClrName { get; set; }


public string Remarks { get; set; }

public bool StatusId { get; set; }


public DateTime StatusChangeDate { get; set; }

public int CreateBy { get; set; }

public DateTime CreatedDate { get; set; }

public int EditBy { get; set; }

public DateTime EditDate { get; set; }

public string Name { get; set; }
public int Length { get; set; }
public string Type { get; set; }
}
}
Clr Controller (Create Part)

public class ClrController : Controller
{
// GET: Masters/Clr
//public ActionResult Index()
//{
// return View();
//}

public FilePathResult Image()
{
string filename = Request.Url.AbsolutePath.Replace("/Clr/image", "");
string contentType = "";
var filePath = new FileInfo(Server.MapPath("~/Uploaded") + filename);

var index = filename.LastIndexOf(".") + 1;
var extension = filename.Substring(index).ToUpperInvariant();

// Fix for IE not handling jpg image types
contentType = string.Compare(extension, "JPG") == 0 ? "image/jpeg" : string.Format("image/{0}", extension);

return File(filePath.FullName, contentType);
}

[HttpPost]
public ContentResult UploadFiles()
{
var r = new List<uploadfilesresult>();

foreach (string file in Request.Files)
{
HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
if (hpf.ContentLength == 0)
continue;

string savedFileName = Path.Combine(Server.MapPath("~/Uploaded"), Path.GetFileName(hpf.FileName));
hpf.SaveAs(savedFileName);

r.Add(new UploadFilesResult()
{
Name = hpf.FileName,
Length = hpf.ContentLength,
Type = hpf.ContentType
});
}
return Content("{\"name\":\"" + r[0].Name + "\",\"type\":\"" + r[0].Type + "\",\"size\":\"" + string.Format("{0} bytes", r[0].Length) + "\"}", "application/json");
}


public ActionResult Save(Clr clr)
{
var objContext = new KktdbContext();
clr.CreateBy = 1;
clr.StatusChangeDate = System.DateTime.Now;
clr.CreatedDate = System.DateTime.Now;
clr.EditBy = 1;
clr.EditDate = System.DateTime.Now;
objContext.Clrs.Add(clr);
objContext.SaveChanges();
TempData["Success"] = "Saved Sucessfully";
return RedirectToAction("ClrIndex", new { A = "New" });


}
View -Create.chtml

@using (@Html.BeginForm("Save", "Clr"))
{

Clr Code *









@ Html.TextBoxFor(a => a.ClCode,new {Class =form-control,placeholder =Clr Code ,TextMode =MultiLine2,onkeyup =return validateChar(this),maxlength =6,style =width:175px; height:25px; margin-top:6px;}}


@Html.TextBoxFor(a => a.ClCode, new { Class = "form-control", placeholder = " Clr Code", TextMode = "MultiLine2", onkeyup = "return validateChar(this)", maxlength = "6", style = "width:175px;height:25px; margin-top:6px;" })



@ Html.ValidationMessageFor(a => a.ClrCode)


@Html.ValidationMessageFor(a => a.ClrCode)










Clr Name *






@ Html.TextBoxFor(a => a.ClrName,new {@maxlength =15,Class =form-control,placeholder =Clr Name ,style =width:175px; height:25px; margin-top:6px;}}

@ Html.ValidationMessageFor(a => a.ClrName)


@Html.TextBoxFor(a => a.ClrName, new { @maxlength = "15", Class = "form-control", placeholder = "Clr Name", style = "width:175px;height:25px;margin-top:6px;" })
@Html.ValidationMessageFor(a => a.ClrName)






















备注




@ Html.TextAreaFor(a => a。备注,新{style =width:250px; resize:none; height:65px ;; margin-top:6px;,Class =form-control,})
@Html.TextAreaFor(a => a.Remarks, new { style = "width: 250px; resize: none; height: 65px;;margin-top:6px;", Class = "form-control", })










状态




@ Html.CheckBoxFor(a => a。 StatusId)
@Html.CheckBoxFor(a => a.StatusId)
















< input type =submitvalue =Saveclass =btn btn-success/>
<input type="submit" value="Save" class="btn btn-success" />



}



<! - 成功保存消息 - >

@if(TempData [成功]!= null)

{


}

<!--Saved Successfully Message-->
@if (TempData["Success"] != null)
{



成功保存


Saved Successfully



}

<! - 结束成功消息 - >




}
<!--End Sved Successfully Message-->







添加文件......

< input id =fileuploadtype =filename =files []multiple onchange =loadFile(event)>









Add files...
<input id="fileupload" type="file" name="files[]" multiple onchange="loadFile(event)">














100%完成(成功)


100% Complete (success)


































< img id =outputstyle =width:200px;身高:200px;/>







< script>

var loadFile = function(event){

var output = document.getElementById('output');

output.src = URL.createObjectURL( event.target.files [0]);

};

< / script>

< script type =text / javascript >

$(document).ready(function(){

$('#fileupload')。fileupload({

dataType :'json',

url:'〜/ Clr / UploadFiles',

autoUpload:true,

done:function(e,数据){

$('。file_name')。html(data.result.name);

$('。file_type')。html(data.result。类型);

$('。file_size')。html(data.result.size);



}

})。on( fileuploadprogressall',function(e,data){

var progress = parseInt(data.loaded / data.total * 100,10);

$('。progress。进度条')。css('width',progress +'%');

});

});

< ; / script>





< script>

$(#output)

.attr('rel','output')

.fancybox({

padding:0

}); < / script>




<img id="output" style="width:200px; height:200px;" />



<script>
var loadFile = function (event) {
var output = document.getElementById('output');
output.src = URL.createObjectURL(event.target.files[0]);
};
</script>
<script type="text/javascript">
$(document).ready(function () {
$('#fileupload').fileupload({
dataType: 'json',
url: '"~/Clr/UploadFiles',
autoUpload: true,
done: function (e, data) {
$('.file_name').html(data.result.name);
$('.file_type').html(data.result.type);
$('.file_size').html(data.result.size);

}
}).on('fileuploadprogressall', function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$('.progress .progress-bar').css('width', progress + '%');
});
});
</script>


<script>
$("#output")
.attr('rel', 'output')
.fancybox({
padding: 0
});</script>

推荐答案

(document).ready(function(){
(document).ready(function () {


('#fileupload')。fileupload({

dataType:'json',

url:'〜/ Clr / UploadFiles ',

autoUpload:true,

done:function(e,data){
('#fileupload').fileupload({
dataType: 'json',
url: '"~/Clr/UploadFiles',
autoUpload: true,
done: function (e, data) {


('。file_name )的.html(d ata.result.name);
('.file_name').html(data.result.name);


这篇关于图像不保存到SQL和文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆