• .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>

  • 相关阅读:
    Windows32位与64位操作系统的区别【转】
    【C#多线程详解】
    auto_ptr
    #if 1......
    vector 向量容器
    删除可视图中的类不能彻底避免它重新被编译
    _tWinMain 与wWinMain 区别
    explicit 用法
    转:atoi函数的实现
    string类的实现
  • 原文地址:https://www.cnblogs.com/lishiyiya/p/13723838.html
Copyright © 2020-2023  润新知