在一组数组或JSON中从Dropzone JS获取所有上传ID



我有一种供人们申请的工作申请。该表格还允许人们上传其简历和求职信。

我使用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);
        }
    }

注意:申请表和上传表格在同一视图中

预先感谢您。

而不是单个ID,您应该返回ID的集合

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);

这将返回IDS的数组

最新更新