using Plupload with ASP.NET/C#

后端 未结 2 1520
Happy的楠姐
Happy的楠姐 2020-12-07 11:15

UPDATE

I was able to get everything to work properly and I just wanted to post back with the updated code. I used Darin Dimitrov\'s suggestion on using a separate

2条回答
  •  一整个雨季
    2020-12-07 12:06

    Here's a full working example I wrote for you:

    <%@ Page Title="Home Page" Language="C#" %>
    <%@ Import Namespace="System.IO" %>
    
    
    
    
        
    
        
        
        
        
        
        
        
        
    
    
    
        

    You browser doesn't have Flash, Silverlight, Gears, BrowserPlus or HTML5 support.

    As you will see in this example files are uploaded to the same page called default.aspx. Notice that parameters such as chunk and name are POSTed so you shouldn't use Request.QueryString to read them but Request["chunk"] directly as this will look at the POST body as well. You should also make sure that the TicketUploads folder exists on the server at the root.

    In this example the same page default.aspx is used for showing the upload form and handling the uploads. In a real world application this is not something I would do. I would recommend you using a separate script which will handle the file uploads such as a generic http handler (upload.ashx).

    Finally you will notice that I have used some default settings that you might wish to modify and reconfigure the plugin to fit your needs. I just took the settings from the documentation.


    UPDATE:

    And since I recommended using a separate generic http handler for handling the file uploads here's how it might look :

    using System.IO;
    using System.Web;
    
    public class Upload : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            int chunk = context.Request["chunk"] != null ? int.Parse(context.Request["chunk"]) : 0;
            string fileName = context.Request["name"] != null ? context.Request["name"] : string.Empty;
    
            HttpPostedFile fileUpload = context.Request.Files[0];
    
            var uploadPath = context.Server.MapPath("~/TicketUploads");
            using (var fs = new FileStream(Path.Combine(uploadPath, fileName), chunk == 0 ? FileMode.Create : FileMode.Append))
            {
                var buffer = new byte[fileUpload.InputStream.Length];
                fileUpload.InputStream.Read(buffer, 0, buffer.Length);
    
                fs.Write(buffer, 0, buffer.Length);
            }
    
            context.Response.ContentType = "text/plain";
            context.Response.Write("Success");
        }
    
        public bool IsReusable
        {
            get { return false; }
        }
    }
    

    Now all that's left is to reconfigure the plugin to point to this generic handler:

    ...
    runtimes: 'gears,flash,silverlight,browserplus,html5',
    url: '/upload.ashx',
    max_file_size: '10mb',
    ...
    

提交回复
热议问题