Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.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)







Remarks

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



Status

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





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

}

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

Saved Successfully



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




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







100% Complete (success)













<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>
Posted
Comments
Afzaal Ahmad Zeeshan 19-Nov-15 5:27am    
Why are you saving the image in database and folder? Both at the same time?

I would recommend that you debug your application and see if the image is passed or not.
MBHGBMB 19-Nov-15 6:07am    
I want to save DB or folder, but not works, not any errors,

thanks for the help, im study it
 
Share this answer
 
Comments
Snesh Prajapati 19-Nov-15 10:01am    
You are most welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900