2013-04-17 52 views
17

Vì vài ngày tôi cố gắng triển khai nhiều tệp tải lên bằng giao diện kéo và thả. Tôi đã tìm kiếm rất nhiều và cuối cùng tìm thấy yêu cầu chính xác của tôi từ http://www.dropzonejs.com/Sử dụng dropzone.js trong asp.net

Tôi đã thử các bước tương tự từ trang web trên. nhưng, tôi không thể triển khai chức năng dropzone này trong trang aspx của mình.

+0

Dưới đây là từng bước bài viết http://codepedia.info/ sử dụng-dropzone-js-file-image-upload-in-asp-net-webform-c/tải lên hình ảnh số lượng lớn bằng cách sử dụng dropzone trong asp.net –

Trả lời

30

Giả sử bạn đang sử dụng Biểu mẫu web, bạn cần triển khai trang đọc dữ liệu tệp được đăng và lưu tệp đó vào tệp.

Ví dụ .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> 

Ví dụ code-behind

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

Nếu bạn đang sử dụng MVC, thấy điều này https://stackoverflow.com/a/15670033/2288997

+7

tôi biết im không giả sử để nói cảm ơn bạn ở đây nhưng thần damn cảm ơn người đàn ông tôi yêu bạn !!!! :) – Liran

+0

Tôi đã thử chính xác như bạn đã trả lời nhưng nó không hoạt động? – fc123

+0

@ fc123: tôi hy vọng bạn thấy bài viết này hữu ích http://codepedia.info/2015/03/using-dropzone-js-file-image-upload-in-asp-net-webform-c/ –

Các vấn đề liên quan