2016-02-28 29 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" }); 
      //} 
     } 

    } 
相關問題