zoukankan      html  css  js  c++  java
  • .net core mvc调用webapi上传图片

    webapi后台

    [HttpPost]
    public int OnPostUpload()
    {
    List<UrlModel> list = new List<UrlModel>();
    var files = Request.Form.Files;
    foreach (var formFile in files)
    {
    if (formFile.Length > 0)
    {
    var fileName = Guid.NewGuid().ToString() + ".jpg";
    string str = AppDomain.CurrentDomain.BaseDirectory;
    string strPath = str.Substring(0, str.IndexOf("bin"));
    var path = Path.Combine(strPath + "images\", fileName);
    using (var stream = new FileStream(path, FileMode.CreateNew))
    {
    formFile.CopyTo(stream);//保存
    UrlModel tu = new UrlModel();
    tu.ImgUrl = @"/images/" + fileName;
    list.Add(tu);
    }
    }
    }

    return list.Count;
    }

    前端mvc

    <form id="uploadForm">
    AJAX上传多文件: <input type="file" name="file" multiple />
    <input type="button" value="上传" onclick="UpLoad()" />
    </form>

    <script>
    function UpLoad() {
    var da = new FormData($("#uploadForm")[0]);
    //da.append(allfiles);
    $.ajax({
    url: 'http://localhost:63285/api/Img/OnPostUpload',
    type: 'post',
    data: da,
    dataType:'text',
    contentType:false,
    processData: false,
    success: function (d) {
    if (d>0) {
    alert("成了");
    }
    }
    })
    }
    </script>

  • 相关阅读:
    记第一场cf比赛(Codeforces915)
    Uva11468:Substring
    Uva11732:"strcmp()" Anyone?
    Uva1014:Remember the Word
    洛谷P2502:[HAOI2006]旅行
    bzoj3677: [Apio2014]连珠线
    bzoj4906: [BeiJing2017]喷式水战改
    海上孤独的帆
    Treap基本用法总结
    noip2017考前基础复习——数论数学
  • 原文地址:https://www.cnblogs.com/lishiyiya/p/13723838.html
Copyright © 2011-2022 走看看