asp.net完成檔案上傳的代碼教程

來源:互聯網
上載者:User
本篇文章主要介紹了asp.net core mvc實現檔案上傳執行個體,具有一定的參考價值,感興趣的小夥伴們可以參考一下。

工作用到檔案上傳的功能,在這個分享下 ~~

Controller:

public class PictureController : Controller  {    private IHostingEnvironment hostingEnv;    public PictureController(IHostingEnvironment env)    {      this.hostingEnv = env;    }    // GET: /<controller>/    public IActionResult Index()    {      return View();    }    public IActionResult UploadFiles()    {      return View();    }    [HttpPost]    public IActionResult UploadFiles(IList<IFormFile> files)    {      long size = 0;      foreach (var file in files)      {        var filename = ContentDispositionHeaderValue                .Parse(file.ContentDisposition)                .FileName                .Trim('"');        //這個hostingEnv.WebRootPath就是要存的地址可以改下        filename = hostingEnv.WebRootPath + $@"\{filename}";        size += file.Length;        using (FileStream fs = System.IO.File.Create(filename))        {          file.CopyTo(fs);          fs.Flush();        }      }      ViewBag.Message = $"{files.Count} file(s) /{ size}bytes uploaded successfully!";       return View();    }  }

view:

<form asp-action="UploadFiles"         asp-controller="Picture"         method="post"         enctype="multipart/form-data">        <input type="file" name="files" multiple />        <input type="submit" value="Upload Selected Files" /> </form>

檔案是上傳到wwwroot目錄檔案下的,這我也看不太懂還在學習,歡迎大家交流~~

----------------------------------------------------------------------------------------------------------

下面是jquery ajax方式上傳的

post方式的action的z參數沒用 因為只有一個post方式的會404錯誤所以又加了一個get的action

Controller:

    public IActionResult UploadFilesAjax()    {      return View();    }    [HttpPost]    public IActionResult UploadFilesAjax(string z)     {      long size = 0;      var files = Request.Form.Files;      foreach (var file in files)      {        var filename = ContentDispositionHeaderValue                .Parse(file.ContentDisposition)                .FileName                .Trim('"');        filename = @"C:\Users\lg.HL\Desktop" + $@"\{filename}";            size += file.Length;        using (FileStream fs = System.IO.File.Create(filename))        {          file.CopyTo(fs);          fs.Flush();        }      }      string message = $"{files.Count} file(s) / { size}bytes uploaded successfully!";        return Json(message);    }

view

<form method="post" enctype="multipart/form-data">      <input type="file" id="files"          name="files" multiple />      <input type="button"          id="upload"          value="Upload Selected Files" /> </form>

jquery

<script type="text/javascript">    $(document).ready(function () {      $("#upload").click(function (evt) {        var fileUpload = $("#files").get(0);        var files = fileUpload.files;        var data = new FormData();        for (var i = 0; i < files.length ; i++) {          data.append(files[i].name, files[i]);        }        $.ajax({          type: "POST",          url: "/Picture/UploadFilesAjax",          contentType: false,          processData: false,          data: data,          success: function (message) {            alert(message);          },          error: function () {            alert("There was error uploading files!");          }        });      });    });</script>

【相關推薦】

1. 精選:“php程式員工具箱”V0.1版本推薦

2. ASP免費視頻教程

3. 李炎恢ASP基礎視頻教程

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.