ASP.net使用JQuery类库ajaxFileUpload实现文件上传

2014-07-23 19:52:40|?次阅读|上传:wustguangh【已有?条评论】发表评论

关键词:JavaScript, JQuery, 界面设计, Web, ASP.NET|来源:唯设编程网

2.服务器端代码实现

服务器端代码是一个名为AjaxUpload.ashx的C#文件:

<%@ WebHandler Language="C#" CodeBehind="AjaxUpload.ashx.cs" Class="AjaxUpload" %>
using System;
using System.Linq;
using LinqHelper;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.IO;


public class AjaxUpload : IHttpHandler  //实现这个接口,意味着要实现该接口的两个方法: ProcessRequest和 IsReusable
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";  //响应数据类型
        if (context.Request.Files.Count > 0 && context.Request.Files[0].ContentLength != 0)  
        {
            try
            {
                decimal id;
                HttpPostedFile file = context.Request.Files[0];   //得到上传文件的对象
                string path = yb.WebHelper.Fetch.MapPath("../../uploadProduceFile");
                //string savefile = Path.Combine(path, Path.GetFileName(file.FileName));
                string ext = "";
                switch (Path.GetExtension(file.FileName))
                {
                    case ".xlsx":
                        ext = ".xls";
                        break;
                    case ".docx":
                        ext = ".doc";
                        break;
                    case ".pptx":
                        ext = ".ppt";
                        break;
                    default:
                        ext = Path.GetExtension(file.FileName);
                     break;
                }
                string DownPath = DateTime.Now.ToFileTime().ToString() + ext;
                string savefile = Path.Combine(path, DownPath);
                //文件信息写入数据库
                using (var db = dbLinq.GetErpData())
                {
                    _AY_AjaxUpFile f = new _AY_AjaxUpFile();
                    f.DownPath = DownPath;
                    f.FileName = Path.GetFileName(file.FileName);
                    f.FileType = Path.GetExtension(file.FileName);
                    f.FileLenth = file.ContentLength.ToString();
                    f.UpMan = "";
                    f.UpTime = System.DateTime.Now;
                    db._AY_AjaxUpFile.InsertOnSubmit(f);
                    try
                    {
                        db.SubmitChanges();
                        id = f.ID;  // you can access your newRecord to get the ID
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }

                file.SaveAs(savefile);  //最核心的地方,实现文件上传
                string result = id.ToString() + "," + DownPath;
                //string result ="{'d':'aaa'}";
                //string result="Hello World";
                HttpContext.Current.Response.Write(result);  //返回最后结果
                //HttpContext.Current.Response.End();

            }
            catch (Exception e)
            {
                context.Response.Write(e);
            }
        }
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

该类使用IHttpHandler接口的ProcessRequest方法处理上传请求,context.Request.Files实现文件处理的核心类,file是一个HttpPostedFile类型的局部变量,通过它调用HttpPostedFile类的SaveAs方法实现文件的上传。最后调用 HttpContext.Current.Response.Write输出执行的结果。该类还使用了linq将文件信息存入数据库中。     

<12>
发表评论0条 】
网友评论(共?条评论)..
ASP.net使用JQuery类库ajaxFileUpload实现文件上传