Search code examples
jqueryasp.netjquery-pluginsuploadify

jquery + asp.net web services file upload


I have an ASP.net webforms site, using jQuery to trigger script methods in asmx web services.
Now I need to add a page which allows a user to upload a file, along with some other properties (like description, creation date etc.)

I understand that there are some jQuery plugins for uploading a file more nicely than the MS file-uploader control (such as uploadify).
however, I can't figure out how to:
1. make it work with script services (I saw some examples using MVC or using http handler, but none using script service
2. In addition to the file itself, I want to be able to send more data along.

any ideas?
thanks


Solution

  • use this one for same task...

    file uploader

    this is code i've written for handler

    <%@ WebHandler Language="C#" Class="Upload" %>
    
    using System;
    using System.Web;
    using System.IO;
    using System.Collections.Generic;
    
    public class Upload : IHttpHandler
    {
    
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                string filePath = "uploads//";
                string newFilename = Guid.NewGuid().ToString();
    
                string nonIEFilename = context.Request.Headers["X-File-Name"];
    
                if (!string.IsNullOrEmpty(nonIEFilename) || context.Request.Files.Count > 0)
                {
                    // if IE
                    if (string.IsNullOrEmpty(nonIEFilename))
                    {
                        HttpPostedFile file = context.Request.Files[0];
                        string[] filenamesplit = file.FileName.ToLower().Split('.');
    
                        newFilename = string.Format("{0}.{1}", newFilename, filenamesplit[1]);
                        file.SaveAs(context.Server.MapPath(string.Format("{0}{1}", filePath, newFilename)));
                        context.Response.Write(string.Format("{{\"path\":\"{0}uploads/{1}\"}}", context.Request.Url.AbsoluteUri.Replace("upload.ashx", string.Empty), newFilename));
                    }
                    else // non IE browsers
                    {
                        string[] filenamesplit = nonIEFilename.ToLower().Split('.');
                        newFilename = string.Format("{0}.{1}", newFilename, filenamesplit[1]);
    
                        using (FileStream filestream = new FileStream(context.Server.MapPath(string.Format("{0}{1}", filePath, newFilename)), FileMode.OpenOrCreate))
                        {
                            Stream inputStream = context.Request.InputStream;
                            inputStream.CopyTo(filestream);
                            context.Response.Write(string.Format("{{\"path\":\"{0}uploads/{1}\"}}", context.Request.Url.AbsoluteUri.Replace("upload.ashx", string.Empty), newFilename));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Errors.addError("Upload.ProcessRequest()", ex);
            }
        }
    
        private bool IsAllowed(string file)
        {
            bool isAllowed = false;
    
            List<string> allowedExtensionsList = new List<string>();
            allowedExtensionsList.Add("jpg");
            allowedExtensionsList.Add("png");
            allowedExtensionsList.Add("gif");
            allowedExtensionsList.Add("bmp");
    
            string[] filenamesplit = file.ToLower().Split('.');
    
            for (int j = 0; j < allowedExtensionsList.Count; j++)
            {
                if (allowedExtensionsList[j] == filenamesplit[1].ToLower())
                {
                    isAllowed = true;
                }
            }
    
            return isAllowed;
        }
    
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    
    }
    

    i have two cases for IE and other browsers because they post files in different forms

    as for additional data, if it's text you can send it via get while calling service