2016-12-30 66 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