为什么列表<>是不是在这段代码中填充?

时间:2016-02-28 20:20:08

标签: c# asp.net ajax dropzone.js

当客户端使用 dropzone js 发布文件时,我正在尝试填充字符串List<string> img = new List<string>();列表中的文件路径 上传的文件没有例外,但列表不会填充,有没有想法解决这个问题?

protected void Page_Load(object sender, EventArgs e)
{
    var httpRequest = System.Web.HttpContext.Current.Request;
    HttpFileCollection uploadFiles = httpRequest.Files;
    List<string> img = new List<string>();

    if (IsPostBack)
    {
        if (httpRequest.Files.Count > 0)
        {
            int i;
            for (i = 0; i < uploadFiles.Count; i++)
            {
                HttpPostedFile postedFile = uploadFiles[i];
                int fileSizeInBytes = postedFile.ContentLength;
                string fileName = postedFile.FileName;// Request.Headers["X-File-Name"];
                string fileExtension = "";

                fileExtension = Path.GetExtension(fileName);
                string savedFileName = Guid.NewGuid().ToString() + fileExtension;
                string path = HttpContext.Current.Server.MapPath("~/img/items/");
                string filename = path + savedFileName;
                postedFile.SaveAs(filename);
                img.Add(filename);
            }
            itm.img1 = img[0];
        }
    }

1 个答案:

答案 0 :(得分:0)

我从

运行他们的项目

https://github.com/venkatbaggu/dropzonefileupload

页面中的js(根据他们的演示)是

 //File Upload response from the server
        Dropzone.options.dropzoneForm = {
            maxFiles: 2,
            url: "WebFormDropzoneDemo.aspx",
            init: function () {
                this.on("maxfilesexceeded", function (data) {
                    var res = eval('(' + data.xhr.responseText + ')');
                });
                this.on("addedfile", function (file) {
                    // Create the remove button
                    var removeButton = Dropzone.createElement("Remove file");
                    // 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.
                        _this.removeFile(file);
                        // If you want to the delete the file on the server as well,
                        // you can do the AJAX request here.
                    });
                    // Add the button to the file preview element.
                    file.previewElement.appendChild(removeButton);
                });
            }
        };

这会填充Request.Files就好了。

public partial class WebFormDropzoneDemo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SaveUploadedFile(Request.Files);
            }
        }


        public void SaveUploadedFile(HttpFileCollection httpFileCollection)
        {
            bool isSavedSuccessfully = true;
            string fName = "";
            foreach (string fileName in httpFileCollection)
            {
                HttpPostedFile file = httpFileCollection.Get(fileName);
                //Save file content goes here
                fName = file.FileName;
                if (file != null && file.ContentLength > 0)
                {

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

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

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

                }

            }

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

    }