【问题标题】:jquery + asp.net web services file uploadjquery + asp.net 网络服务文件上传
【发布时间】:2011-08-01 09:48:05
【问题描述】:

我有一个 ASP.net webforms 站点,使用 jQuery 来触发 asmx web 服务中的脚本方法。
现在我需要添加一个允许用户上传文件的页面,以及其他一些属性(如描述、创建日期等)

我知道有一些 jQuery 插件可以比 MS 文件上传器控件更好地上传文件(例如 uploadify)。
但是,我不知道如何:
1. 使其与脚本服务一起工作(我看到了一些示例using MVC 或使用http handler,但没有使用脚本服务
2. 除了文件本身,我希望能够发送更多的数据。

有什么想法吗?
谢谢

【问题讨论】:

    标签: jquery asp.net jquery-plugins uploadify


    【解决方案1】:

    将这个用于相同的任务...

    file uploader

    这是我为处理程序编写的代码

    <%@ 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;
            }
        }
    
    }
    

    IE 和其他浏览器有两种情况,因为它们以不同的形式发布文件

    至于附加数据,如果是文本,您可以在调用服务时通过get发送它

    【讨论】:

    • 我最终做了一些非常相似的事情,使用 http 处理程序并使用 uploadify - 它工作得非常好。谢谢
    最近更新 更多