zoukankan      html  css  js  c++  java
  • (不适用.Net Core)layui+WebApi上传文件、上传图片

    前言:这里举例是上传文件、上传图片同理!

    注意:layui.js版本必须使用2.0版本以上,否则线上会出现上传跨域(CORS)问题!

    一、 layu0 JSi声明、调用

    layui.use('upload',function()
    {
      var upload=layui.upload;
        //执行实例
      var uploadInst = upload.render({
        elem: '#test1' //绑定元素
        ,url: '/upload/' //上传接口
        , accept: 'file'//允许上传的文件类型
        , multiple: true
        , auto: true
        ,done: function(res){
          //上传完毕回调
        }
        ,error: function(){
          //请求异常回调
        }
      });
    
    })

    二、WebAPI接收上传的文件信息,并处理

     public IHttpActionResult Get()
            {
                if (HttpContext.Current.Request.Files.Count > 0)
                {
                    try
                    {
                        string filePath = "~/File/demo/";
                        //得到客户端上传的文件
                        HttpPostedFile file = HttpContext.Current.Request.Files[0];
                        //如果文件不存在就重新创建一个
                        if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath(filePath)))
                        {
                            Directory.CreateDirectory(HttpContext.Current.Server.MapPath(filePath));
                        }
                        //服务器端要保存的路径
                         filePath = HttpContext.Current.Server.MapPath(filePath) + file.FileName;// +".txt";
                        file.SaveAs(filePath);
                        //返回结果
                        return Json(new { res = "success", filePath = "~/File/demo/" + file.FileName });
                    }
                    catch (Exception ex)
                    {
                        return Json(new { res = ex.Message });
                    }
                }
                else
                {
                    return Json(new { res = "error" }); ;
                    //Response.Write("Error1");
                }
            }
  • 相关阅读:
    The 2014 ACM-ICPC Asia Xi'an Regional Contest — F题 Color
    CodeForces 358D — Dima and Hares
    VIJOS国庆节模拟赛之繁星春水
    两个算法
    HDU 4901
    Andrew Stankevich Contests #2
    HDU 4701
    HDU 5033
    程序安装出现错误代码为2869
    常用正则表达式总结(以后加了再补充)
  • 原文地址:https://www.cnblogs.com/XiangZiPeng/p/12195887.html
Copyright © 2011-2022 走看看