2011-08-01 66 views
0

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

据我所知,有一些jQuery插件更多好听上传文件一起比MS文件上传器控件(例如uploadify)。
然而,我无法弄清楚如何:
1.使其与脚本服务工作(我看到一些例子using MVC或使用http handler,但没有使用脚本服务
2.除了文件本身,我希望能够沿着发送更多的数据。

什么想法?
感谢

回答

0

使用这一个相同的任务...

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和其他浏览器,因为它们以不同的形式

为其他数据文件发布,如果它是你可以发送文本它通过调用服务时获得

+0

我最终做了一件非常相似的事情,使用http处理程序并使用uploadify - 它工作得非常好。谢谢 –