当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net实现文件无刷新上传方法汇总

asp.net实现文件无刷新上传方法汇总

2017年12月12日  | 移动技术网IT编程  | 我要评论

407不许闹,车侑兰,身边的科学手抄报

遇到上传文件的问题,结合之前用到过的swfupload,又找了一个无刷新上传文件的jquery插件uploadify,写篇博客记录一下分别介绍这两个的实现方法

swfupload 导入swfupload的开发包 添加js引用,引用swfupload.js与handler.js文件,如果对swfupload不了解、有疑问可以看看页面初始化

修改handler.js文件中 上传成功的事件,serverdata是服务器端的响应

uploadify 导入uploadify开发包,从官网,,, 添加js与css的引用,jquery.uploadify.js 、uploadify.css

(注:在css中引用uploadify-cancel.png图片文件的路径是可能不正确,可以在uploadify.css文件中自己进行更改)

页面初始化

页面初始化时,可以指定许多设置,并对上传成功的事件进行重载,data表示服务器端的响应

服务器端上传处理程序

//uploadify初始化
    $(function () {
      $('#file_upload').uploadify({
        //指定swf
        'swf': '/uploadify/uploadify.swf',
        //服务器端处理程序
        'uploader': '/admin/uploadfilehandler.ashx',
        //按钮文本
        buttontext: '上传附件',
        //文件类型
        filetypeexts: "*.zip;*.rar;*.doc;*.docx;*.xls;*xlsx",
        onuploadsuccess: onfileuploadsuccess
      });
    });
    function onfileuploadsuccess(file, data, response) {
      //服务器端响应
      if (data == 'nopermission') {
        alert('没有上传权限');
      }
      if (data == 'error') {
        alert('上传失败');
      } else if (response) {
        alert('上传成功~~~');
        $("#filepath").val(data);
      }
    }

uploadify
/// <summary>
/// 上传文件
/// </summary>
public class uploadfilehandler : ihttphandler, irequiressessionstate
{
  public void processrequest(httpcontext context)
  {
    context.response.contenttype = "text/plain";
    //验证上传权限
    if (context.session["user"] == null)
    {
      context.response.write("no permission");
      context.response.end();
      return;
    }
    try
    {
      //获取上传文件
      //filedata是客户端已经定义好的,如果想要更改,更改js文件中的配置
      httppostedfile image_upload = context.request.files["filedata"];
      //获取文件扩展名
      string fileext = system.io.path.getextension(image_upload.filename).tolower();
      //验证文件扩展名是否符合要求,是否是允许的图片格式
      if (!filetypes.isallowed(fileext))
      {
        return;
      }
      //当前时间字符串
      string timestring = datetime.now.tostring("yyyymmddhhmmssfff");
      //保存虚拟路径构建
      string path = "/upload/" + timestring + fileext;
      //获取、构建要上传文件的物理路径
      string serverpath = context.server.mappath("~/" + path);
      //保存图片到服务器
      image_upload.saveas(serverpath);
      //输出保存路径
      context.response.write(path);
    }
    catch (exception ex)
    {
      context.response.write("error");
      //记录日志
      new common.loghelper(typeof(uploadfilehandler)).error(ex);
    }
  }

  public bool isreusable
  {
    get
    {
      return false;
    }
  }
}
public static class filetypes
{
  private static list<string> allowedfiletypes = new list<string>();
  //获取允许json配置文件
  private static string jsonfilepath = common.pathhelper.mappath("~/allowedfiletypes.json");
  
  /// <summary>
  /// 允许的文件类型
  /// </summary>
  public static list<string> allowedfiletypes
  {
    get
    {
      return allowedfiletypes;
    }

    set
    {
      allowedfiletypes = value;
    }
  }

  /// <summary>
  /// 静态构造方法
  /// </summary>
  static filetypes()
  {
    loadfiletypesfromjson();
  }

  /// <summary>
  /// 从json文件中读取允许上传的文件类型
  /// </summary>
  private static void loadfiletypesfromjson()
  {
    string types = file.readalltext(jsonfilepath);
    allowedfiletypes = common.converterhelper.jsontoobject<list<string>>(types);
  }

  /// <summary>
  /// 当添加允许文件类型时,更新到json文件
  /// </summary>
  public static void filetypestojson()
  {
    string types = common.converterhelper.objecttojson(allowedfiletypes);
    file.writealltext(jsonfilepath, types);
  }

  /// <summary>
  /// 新增允许上传文件扩展名
  /// </summary>
  /// <param name="newfiletype"></param>
  public static void addnewfiletype(string newfiletype)
  {
    allowedfiletypes.add(newfiletype);
    filetypestojson();
  }
  
  /// <summary>
  /// 判断某种文件类型是否允许上传
  /// </summary>
  /// <param name="fileext">文件扩展名</param>
  /// <returns>是否允许上传<code>true</code>允许上传</returns>
  public static bool isallowed(string fileext)
  {
    foreach (string item in allowedfiletypes)
    {
      if (fileext.equals(fileext))
      {
        return true;
      }
    }
    return false;
  }
}

以上所述就是本文的全部内容了,希望大家能够喜欢。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网