如何从特定用户

时间:2015-04-28 10:26:39

标签: javascript jquery asp.net-mvc dropzone.js

我在创建表单中使用此http://www.dropzonejs.com/ 当用户单击“单击此处添加文件”时,文件将存储在Files / TempFile中 但是当用户在我的create方法上单击“提交”时,我想将所有文件从Files / TempFile移动到从用户上传的Files / TicketFile,或者如果用户单击“取消”从Files / TempFile中删除所有文件。 问题是,如果有多个用户尝试同时上传文件。如果其中一个用户单击取消或提交如何知道要移动或删除的文件。

创建视图

@{
ViewBag.Title = "Create";
}

@using (Html.BeginForm("Create", "Ticket", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
    <legend>Ticket</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.idTicket)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.idTicket)
        @Html.ValidationMessageFor(model => model.idTicket)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.idProject, "Project")
    </div>
    <div class="editor-field">
        @Html.DropDownList("idProject", String.Empty)
        @Html.ValidationMessageFor(model => model.idProject)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.tickettDescription)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.tickettDescription)
        @Html.ValidationMessageFor(model => model.tickettDescription)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.assignment, "User")
    </div>
    <div class="editor-field">
        @Html.DropDownList("assignment")
        @Html.ValidationMessageFor(model => model.assignment)
    </div>
    <div class="jumbotron">
        <div class="dropzone" id="dropzoneForm" style="width: 50px; background: none; border: none;">
            <div class="fallback">
                <input type="file" id="fileInput" name="files" multiple="multiple" >
                <input type="submit" id="submit" value="Upload" />
            </div>
        </div>
    </div>


    <div class="clear-fix"></div>
    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
}


<div>
    @Html.ActionLink("Back to List", "Index")
</div>

<script type="text/javascript">
    //File Upload response from the server
    Dropzone.options.dropzoneForm = {

        init: function () {
            this.on("maxfilesexceeded", function (data) {
                $.ajax({

                    url: '@Url.Action("SaveUploadedFile", "File", new { id=1})',
                })
                var res = eval('(' + data.xhr.responseText + ')');
            });
            this.on("addedfile", function (file) {
                // Create the remove button
                var removeButton = Dropzone.createElement("<button>Remove file</button>");

                // Capture the Dropzone instance as closure.
                var _this = this;

                // Listen to the click event
                removeButton.addEventListener("click", function (e) {

                    // Make sure the button click doesn't submit the form:
                    e.preventDefault();
                    e.stopPropagation();
                    // Remove the file preview.
                    $.ajax({
                        type: "POST",
                        url: '@Url.Action("RemoveFile","File")',
                        contentType: "application/json; charset=utf-8",
                        data: "{name:" + JSON.stringify(file.name) + "}",
                        dataType: "json",
                        success: function () { _this.removeFile(file); }

                    });
                })

                // Add the button to the file preview element.
                file.previewElement.appendChild(removeButton);
            });
        }
    };

</script>

保存方法

 public ActionResult SaveUploadedFile()
    {
        bool isSavedSuccessfully = true;

        string fName = "";
        try
        {
            foreach (string fileName in Request.Files)
            {
                HttpPostedFileBase file = Request.Files[fileName];
                //Save file content goes here
                fName = file.FileName;
                if (file != null && file.ContentLength > 0)
                {
                    string path = Server.MapPath("~/Files/TempFile/") + file.FileName;
                    file.SaveAs(path);

                }

            }

        }
        catch (Exception ex)
        {
            isSavedSuccessfully = false;
        }


        if (isSavedSuccessfully)
        {
            return Json(new { Message = fName });
        }
        else
        {
            return Json(new { Message = "Error in saving file" });
        }
    }

我尝试不存储到TempFile,当我点击创建以获取所有文件

 foreach (string fileName in Request.Files)

但Request.Files始终为null。

1 个答案:

答案 0 :(得分:0)

将文件上传到Files / TempFile时,会向其添加dummmy后缀,以识别正在上传文件的用户,例如。如果用户说&#34; ABC&#34;正在上传文件&#34; File1&#34;然后在你的代码中上传文件重命名文件为&#34; File1_ABC&#34;。同样,如果用户&#34; PQR&#34;正在上传&#34; File1&#34;那么上传的文件夹应该是&#34; File1_PQR&#34;现在当用户&#34; ABC&#34;单击您的创建方法移动文件从文件/ TempFile到提交后缀为&#34; _ABC&#34;的文件/ TicketFile。