2016-02-28 28 views
1

我想填充文件路径的字符串列表List<string> img = new List<string>();当有文件张贴由客户端使用dropzone js 有没有例外的文件上传,但列表将不会填充,任何想法来解决这个问题?为什么列表<>不在此代码中填充?

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]; 
     } 
    } 
+0

运行代码后'img.Count'的值是多少?你怎么知道它不会填充?你有例外吗?需要更多信息。 –

+0

@LukePark好的我会编辑我的问题 – ajjawi

+0

@LukePark运行此代码后,@ imke.Count'为0 – ajjawi

回答

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" }); 
      //} 
     } 

    } 
相关问题