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