在asp.net中使用dropzone.js

本文关键字:dropzone js asp net | 更新日期: 2023-09-27 18:09:38

由于几天我试图实现多个文件上传与拖放界面。我找了很多,终于找到了我的确切要求http://www.dropzonejs.com/

我从上面的站点尝试了相同的步骤。但是,我无法在我的aspx页面中实现这个dropzone功能。

在asp.net中使用dropzone.js

假设您正在使用Web Forms,您需要实现一个页面来读取提交的文件数据并将其保存到文件中。

. 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