WebApi使用二進制方式上傳和下載文件


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using System.Text;
using Tallsafe.BigStorage.Core;
using Microsoft.AspNetCore.Authorization;
using System.Reflection;
using System.Net.Http;
using System.Net;
using System.Net.Http.Headers;

namespace TT.Storage.Controllers
{
    //[Authorize]
    [Route("api/[controller]")]
    public class StorageFileController : Controller
    {
        // GET api/values/5
        [Route("GetContent")]
        [HttpGet()]
        public IActionResult GetContent(string fileInfo)
        {
            byte[] content = GetFileInfo(fileInfo).FileContent;;

           return  new FileContentResult(content, "application/octet-stream");
        }

        // POST api/values
        [Route("AddFile")]
        [HttpPost]
        public StoreFileInfo AddFile([FromBody]byte[] value)
        {
            if (!Request.ContentLength.HasValue ||
                Request.ContentLength <= 0)
            {
                return null;
            }

            var contentLength = (int)Request.ContentLength;

            byte[] buffer = new byte[contentLength];

            Request.Body.Read(buffer, 0, contentLength);
            
            return StorageClassCenter.Store(buffer);
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM