2013-04-17 25 views
17

由於我試圖通過拖放界面實現多個文件上載的幾天。 我搜查了很多,最後發現我確切的要求從 http://www.dropzonejs.com/在asp.net中使用dropzone.js

我嘗試了上述網站的相同步驟。 但是,我無法在我的aspx頁面中實現這個dropzone功能。

+0

這裏是一步一步的文章http://codepedia.info/使用dropzone-js-file-image-upload-in-asp-net-webform -c/upload在asp.net中使用dropzone的批量圖像 –

回答

30

假設您使用的是Web窗體,您需要實現一個頁面來讀取發佈的文件數據並將其保存到文件中。

實例.ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Mvc4Application_Basic.WebForm1" %> 

    <!DOCTYPE html> 

    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head runat="server"> 
     <title></title> 
     <script src="https://raw.github.com/enyo/dropzone/master/downloads/dropzone.js"></script> 
     <link href="http://www.dropzonejs.com/css/general.css?v=7" rel="stylesheet" /> 
    </head> 
    <body> 
     <form id="frmMain" runat="server" class="dropzone"> 
      <div> 
       <div class="fallback"> 
        <input name="file" type="file" multiple /> 
       </div> 
      </div> 
     </form> 
    </body> 
    </html> 

示例代碼隱藏

public partial class WebForm1 : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      foreach (string s in Request.Files) 
      { 
       HttpPostedFile file = Request.Files[s]; 

       int fileSizeInBytes = file.ContentLength; 
       string fileName = Request.Headers["X-File-Name"]; 
       string fileExtension = ""; 

       if (!string.IsNullOrEmpty(fileName)) 
        fileExtension = Path.GetExtension(fileName); 

       // IMPORTANT! Make sure to validate uploaded file contents, size, etc. to prevent scripts being uploaded into your web app directory 
       string savedFileName = Path.Combine(@"C:\Temp\", Guid.NewGuid().ToString() + fileExtension); 
       file.SaveAs(savedFileName); 
      } 
     } 
    } 

如果您正在使用MVC,看到這個https://stackoverflow.com/a/15670033/2288997

+7

我知道我不想在這裏說謝謝,但上帝該死的感謝男人我愛你!!!! :) – Liran

+0

我嘗試完全按照你的回答,但它不工作? – fc123

+0

@ fc123:我希望你找到這篇文章有幫助http://codepedia.info/2015/03/using-dropzone-js-file-image-upload-in-asp-net-webform-c/ –