Manoj Maharana

Manoj Maharana

  • NA
  • 362
  • 128k

Mvc Fileupload controler not save in document

Mar 17 2017 12:14 PM

How do i save a image in folder in mvc 4.

Here is the code:
 
 
  1. <form method="post" class="form-horizontal" name="NewLabelform" enctype="multipart/form-data">  
  2.                     <div class="modal-header">  
  3.                         <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>  
  4.                         <h4 class="modal-title" id="myModalLabel">Add New Ticket</h4>  
  5.                     </div>  
  6.                     <div class="modal-body">  
  7.                         <p>  
  8.                             <span>Type:</span>  
  9.                             <select class="form-control" id="addtype">  
  10.                                 <option value="1">Technical Issue</option>  
  11.                                 <option value="2">Bug</option>  
  12.                                 <option value="3">Feature Request</option>  
  13.                                 <option value="4">Sales Question</option>  
  14.                                 <option value="5">How To</option>  
  15.                             </select>  
  16.                             <span id="errortype" style="color: Red;"></span>  
  17.                         </p>  
  18.                         <p>  
  19.                             <span>Title:</span>  
  20.   
  21.                             <input type="text" class="form-control" id="addTitle" />  
  22.                             <span id="errorTitle" style="color: Red;"></span>  
  23.                         </p>  
  24.                         <p>  
  25.                             <span>description:</span>  
  26.   
  27.                             <textarea rows="4" cols="50" class="form-control" id="addDesc"></textarea>  
  28.                             <span id="errorDesc" style="color: Red;"></span>  
  29.                         </p>  
  30.                         <p>  
  31.                             <span>Importancy:</span>  
  32.                             <select class="form-control" id="addimportancy">  
  33.                                 <option value="1">High</option>  
  34.                                 <option value="2">Medium</option>  
  35.                                 <option value="3">Low</option>  
  36.   
  37.                             </select>  
  38.                             <span id="errorimportancy" style="color: Red;"></span>  
  39.                         </p>  
  40.                         <p>  
  41.   
  42.                                 <span>Attached Documents:</span>  
  43.             <input type="file" name="fileuploader" class="form-control" id="fileuploader" />  
  44.             <span id="errorAttach" style="color: Red;"></span>  
  45.   
  46. </p>  
  47.   
  48.   
  49.                     </div>  
  50.                     <div class="modal-footer">  
  51.                         <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>  
  52.                         <button type="button" class="btn btn-primary" onclick="CreateLabeData()">Create</button>  
  53.                     </div>  
  54.                 </form>  
 
Here is the Ajax Method:
 
  1. <script>  
  2. var $al = jQuery.noConflict();  
  3. function CreateLabeData() {  
  4.     debugger  
  5.     var errortype = document.getElementById('errortype');  
  6.   
  7.     var addtype = document.getElementById('addtype');  
  8.     if (addtype.length == "0") {  
  9.         errortype.innerHTML = "Type is required.";  
  10.         addtype.focus();  
  11.         return false;  
  12.     }  
  13.     else {  
  14.         errortype.innerHTML = "";  
  15.     }  
  16.     var errorTitle = document.getElementById('errorTitle');  
  17.     var addTitle = document.getElementById('addTitle');  
  18.     if (addTitle.value == '') {  
  19.         errorTitle.innerHTML = "Title is required.";  
  20.         addTitle.focus();  
  21.         return false;  
  22.     }  
  23.     else {  
  24.         errorTitle.innerHTML = "";  
  25.     }  
  26.     var errorDesc = document.getElementById('errorDesc');  
  27.     var addDesc = document.getElementById('addDesc');  
  28.     if (addDesc.value == '') {  
  29.         errorDesc.innerHTML = "Description is required.";  
  30.         addDesc.focus();  
  31.         return false;  
  32.     }  
  33.     else {  
  34.         errorDesc.innerHTML = "";  
  35.     }  
  36.   
  37.     var errorimportancy = document.getElementById('errorimportancy');  
  38.     var addimportancy = document.getElementById('addimportancy');  
  39.     if (addimportancy.length == "0") {  
  40.         errorimportancy.innerHTML = "Importancy is required.";  
  41.         addimportancy.focus();  
  42.         return false;  
  43.     }  
  44.     else {  
  45.         errorimportancy.innerHTML = "";  
  46.     }  
  47.     //var foldername = document.getElementById("fname").value;  
  48.     var readdtype = document.getElementById('addtype').value;  
  49.     var readdTitle = document.getElementById('addTitle').value;  
  50.     var readdDesc = document.getElementById('addDesc').value;  
  51.     var readdimportancy = document.getElementById('addimportancy').value;  
  52.     //var fname = document.querySelector('input[type=file]').files[0].name;  
  53.     //var doc = $al("#fileuploader").val();  
  54.     //var filename = doc.replace(/^.*[\\\/]/, '');  
  55.   
  56.     //var formData = new FormData();  
  57.     //var totalFiles = document.getElementById("fileuploader").files.length;  
  58.     //for (var i = 0; i < totalFiles; i++) {  
  59.     //    var file = document.getElementById("fileuploader").files[i];  
  60.   
  61.     //    formData.append("fileuploader", file);  
  62.     //}  
  63.     //formData.append("fileuploader", file);  
  64.   
  65.   
  66.     $.ajax(  
  67.         {  
  68.             //url: "/Ticket/InsertTicket/",  
  69.             url: '@Url.Content("~/TicketTemplate/InsertTicket")',  
  70.             type: "POST",  
  71.             cache: false,  
  72.             async: true,  
  73.             datatype: "json",  
  74.             contentType: 'application/json; charset=utf-8',  
  75.             //data: JSON.stringify({ 'Addtype': readdtype, 'AddTitle': readdTitle, 'AddDesc': readdDesc, 'Addimportancy': readdimportancy, 'FileName': filename,' FormData': formData }),  
  76.             data: JSON.stringify({ 'Addtype': readdtype, 'AddTitle': readdTitle, 'AddDesc': readdDesc, 'Addimportancy': readdimportancy }),  
  77.             success: function (result) {  
  78.                 debugger;  
  79.                 if (result.isSuccess) {  
  80.                     window.location.reload(true);  
  81.                 }  
  82.                 else {  
  83.   
  84.                     alert('!');  
  85.                 }  
  86.             },  
  87.             error: function (result) {  
  88.                 debugger;  
  89.                 alert('');  
  90.             }  
  91.         });  
  92. }  
  93. Here is the controller:  
  94.   
  95.   public ActionResult InsertTicket(HttpPostedFileBase FileName, string Addtype, string AddTitle, string AddDesc, string Addimportancy, string FormData)  
  96.     //public ActionResult InsertTicket( string Addtype, string AddTitle, string AddDesc, string Addimportancy)  
  97.     {  
  98.         string isSuccess = "0";  
  99.   
  100.         AppTicket models;  
  101.   
  102.         AppTicket model = new AppTicket();  
  103.         model.Type = Convert.ToInt32(Addtype);  
  104.         model.Title = Convert.ToString(AddTitle);  
  105.         model.Description = Convert.ToString(AddDesc);  
  106.         model.Importancy = Convert.ToInt32(Addimportancy);  
  107.         //obj.Title = Convert.ToString(AddTitle);  
  108.         int CompanyId = 1;  
  109.   
  110.         int status = 1;  
  111.   
  112.         //if (FileName != null)  
  113.         //{  
  114.         //    string saveto = string.Empty;  
  115.         //   // var File = FileName;  
  116.         //    //saveto = Path.Combine(Server.MapPath("~/Content/Areas/Ticket/Content"), FileName);  
  117.         //  //  File.SaveAs(saveto);  
  118.   
  119.   
  120.         //}  
  121.         //var file = Request.Files;  
  122.         ////string saveto = string.Empty;  
  123.         //string name = string.Empty;  
  124.         //if (Request.Files.Count > 0)  
  125.         //{  
  126.         //    var File = Request.Files[0];  
  127.         //    Random rnd = new Random();  
  128.         //    name = rnd.Next(111, 9999).ToString() + "_" + System.IO.Path.GetFileName(File.FileName);  
  129.         //    saveto = Path.Combine(Server.MapPath("~/Content/Areas/Ticket/Content"), name);  
  130.         //    File.SaveAs(saveto);  
  131.         //    // Session["File"] = name;  
  132.         //    Session["File"] = saveto;  
  133.         //}  
  134.   
  135.   
  136.   
  137.         Int64? res = _apiTicket.Posts(model.Title, (model.Type), model.Description, (model.Importancy), "", (UserId), CompanyId, false, status);  
  138.         if (res > 0)  
  139.         {  
  140.             isSuccess = "1";  
  141.         }  
  142.         return Json(isSuccess, JsonRequestBehavior.AllowGet);  
  143.     }  
 
 

How do i save uploaded file in folder??

I have tried a lot but this is not working for me...any suggestion??

In a model popup i need to show the form having all these component.

When click on save it need to save on the folder and name in db.

But it is not working??

So any one can try or give me some solutions??

 
I have try to call through the ajax method not working. 

Answers (2)