【问题标题】:Using dropzone.js in asp.net在 asp.net 中使用 dropzone.js
【发布时间】:2013-04-09 16:18:49
【问题描述】:

几天以来,我一直在尝试通过拖放界面实现多文件上传。 我搜索了很多,最后找到了我的确切要求 http://www.dropzonejs.com/

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

【问题讨论】:

    标签: c# javascript jquery asp.net


    【解决方案1】:

    假设您使用的是 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

    【讨论】:

    • 我知道我不应该在这里说谢谢,但该死的谢谢,我爱你!!!! :)
    • 我在按钮单击时使用您的方法,但 Request.Files 得到空值,如果可能,请暗示感谢@Arsen
    猜你喜欢
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    • 2018-07-27
    • 2013-12-21
    • 1970-01-01
    • 1970-01-01
    • 2014-06-30
    • 2017-04-21
    相关资源
    最近更新 更多