2016-12-30 67 views
0

我有一種工作申請表供人申請。該表格還允許人們上傳他們的簡歷和求職信。從Dropzone Js中獲取所有上傳ID的數組或JSON集合

我使用Dropzone上傳多個文件(簡歷和封面字母)。我想獲得一組已經上傳到數組或json的ID。所以,我可以用它來與職位申請表

var idResult = ""; 
    Dropzone.options.uploadDemo = { 
     maxFilesize: 5, //accept file size 5MB 
     paramName: "file", // The name that will be used to transfer the file 
     acceptedFiles: ".pdf,.doc,.docx,.docm,.docb,.dotx,.dotm", //acccept file extension 
     addRemoveLinks: true, 
     init: function() { 
      this.on("success", function (data) { 
       console.log(data); 
       console.log(data.xhr.response); 
       $('#@Html.IdFor(x=>x.Applicant.UploadIds)').val(data.UploadIds); 
      }); 

在這裏創建的關係是我的控制器

[HttpPost] 
    [Route("Career/SaveUploadedFile")] 
    public ActionResult SaveUploadedFile() 
    { 
     try 
     { 
      string uploadId = string.Empty; 
      foreach (string fileName in Request.Files) 
      { 
       HttpPostedFileBase file = Request.Files[fileName]; 
       //Save file content goes here 

       if (file != null && file.ContentLength > 0) 

       { 

        var originalDirectory = new DirectoryInfo(string.Format("{0}Upload\\Document", Server.MapPath(@"\"))); 

        string pathString = System.IO.Path.Combine(originalDirectory.ToString(), "file"); 

        var fileName1 = Path.GetFileName(file.FileName); 

        bool isExists = System.IO.Directory.Exists(pathString); 

        if (!isExists) 
         System.IO.Directory.CreateDirectory(pathString); 

        var path = string.Format("{0}\\{1}", pathString, file.FileName); 

        file.SaveAs(path); 


        var upload = TTCHRFacade.CreateUpload(new Upload() 
        { 
         FileName = file.FileName, 
         FileSize = file.ContentLength, 
         FileExtention = file.ContentType, 
         GUIDFileName = Guid.NewGuid().ToString(), 
         UploadDate = DateTime.Now 
        }); 
        uploadId += upload.UploadId.ToString() + ","; 
        //return PartialView("ApplicantUploadTemplate", upload); 
       } 
       else 
       { 
        // Set as bad request (400) 
        Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest; 
        ModelState.AddModelError("error", "Need Files."); 
        return Json(ModelState.Values.FirstOrDefault().Errors.FirstOrDefault().ErrorMessage); 
       } 
      } 
      return Json(new { uploadId }, JsonRequestBehavior.AllowGet); //TODO 

     } 
     catch (Exception ex) 
     { 
      // Set as bad request (400) 
      Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest; 
      return Json(ModelState.Values.FirstOrDefault().Errors.FirstOrDefault().ErrorMessage); 
     } 
    } 

注:申請表,並上傳表單都在同一視圖

謝謝你提前。

+0

'data.uploadId'應給你新的身份證。 – Shyju

+0

是的。但它不是一組id。 –

+0

您可以返回一個ID數組。查看發佈的答案。 – Shyju

回答

0

而不是一個單一的ID,你應該在傳遞給你的Ajax方法的成功調用返回數據的標識的

var uploadIds = new List<string>(); 
foreach (string fileName in Request.Files) 
{ 
    // Your existing code goes here 
    var uploadId = upload.UploadId.ToString(); 
    uploadIds.Add(uploadId); 
} 
//Now return the list of Ids 
return Json(uploadIds, JsonRequestBehavior.AllowGet); 

集合這將返回一組ID

+0

它看起來像在工作。但是,我無法在查看頁面中返回結果。 :3 –

+0

你想在哪裏返回查看頁面的結果?什麼結果? – Shyju