Asp.net core 學習筆記 ( upload/download files 文件上傳與下載 )

2017-09-25 

refer : https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads

https://www.codeproject.com/Articles/1203408/Upload-Download-Files-in-ASP-NET-Core

這裡只說說小文件上傳. 

先看看前端 js 程式碼 

<input id="inputFile" type="file" />
<script>
    document.getElementById('inputFile').addEventListener('change', (e) => {        
        let files = e.target.files;        
        let formData = new FormData();
        formData.append("file", files[0]);
        let http = new XMLHttpRequest();
        http.open('POST', '/upload-file', true);
        http.send(formData); 
    }, false); 
</script>

如果要上傳多個文件就 append 多幾個就是了 

c# 

public class UploadFileData
{
    public IFormFile file { get; set; }
}

[Area("Web")]
public class UploadController : Controller
{
    public UploadController(
            IHostingEnvironment environment
    )
    {
        this.environment = environment;
    }

    private IHostingEnvironment environment { get; set; }
        
    [HttpPost("upload-file")]
    public async Task<IActionResult> uploadFile(UploadFileData data)
    {
        var allFiles = Request.Form.Files; // 多文件的話可以直接從 form 拿到完, 或則設定成 List<IFormFile> 就可以了
        var root = environment.WebRootPath;
        var extension = Path.GetExtension(data.file.FileName);
        var guid = Guid.NewGuid().ToString();
        var fullPath = $@"{root}\images\{guid + extension}";
        using (FileStream stream = new FileStream(fullPath, FileMode.Create))
        {
            await data.file.CopyToAsync(stream);
        } 
        return Ok();
    }
    [Route("upload")]
    public async Task<IActionResult> Index()
    {
        return View();
    }
}

上面的是最簡單的版本,建立 file 然後把 upload file stream 寫入 

如果是想直接使用 stream 也是可以 

using (var memoryStream = new MemoryStream())
{
    await model.file.CopyToAsync(memoryStream);
    memoryStream.Seek(0, SeekOrigin.Begin);
    new FileExtensionContentTypeProvider().TryGetContentType(model.file.FileName, out string contentType);

    await EmailService.SendAsync(
        recipients: BusinessConfig.contactEmailHandler,
        subject: "Contact form from website",
        templatePath: "~/Email/Contact/Index.cshtml",
        model: model,
        attachs: new List<Attachment> { new Attachment(memoryStream, model.file.FileName, contentType ?? "application/octet-stream") }
    );
}

需要注意的是寫入 stream 後, 如果要讀取 stream 記得, Seek 一下. 

 

還有一個常用的場景是, 上傳圖片要做 EXIF 處理. 

可以用 Magick.NET 目前支援 core, 不過呢, 好像只支援 window 場景. 如果你不是 windows 可能要在等等或則用其它外掛.

Magick 需要設定, 參考 : https://magick.codeplex.com/documentation

MagickAnyCPU.CacheDirectory = @"C:\MyProgram\MyTempDir";
MagickNET.SetTempDirectory(@"C:\MyProgram\MyTempFiles");
例子
using (var stream = data.file.OpenReadStream())
using (MagickImage image = new MagickImage(stream))
{
    ExifProfile profile = image.GetExifProfile();
    image.Settings.SetDefine(MagickFormat.Jpeg, "sampling-factor", "4:2:0");
    image.Strip(); //這句會把圖片的所有 EXIF 洗掉
    image.Quality = 85;

    if (profile != null)
    {
        ExifValue orientation = profile.Values.SingleOrDefault(v => v.Tag == ExifTag.Orientation);
        if (orientation != null)
        {
            int orientationInt = Convert.ToInt32(orientation.Value);
            if (orientationInt == 6)
            {
                image.Rotate(90);
            }
            else if (orientationInt == 8)
            {
                image.Rotate(-90);
            }
            else if (orientationInt == 8)
            {
                image.Rotate(180);
            }
        }
        image.Write(fullPath);
    }
    else
    {
        image.Write(fullPath);
    }
}

很簡單吧.

再來一個 zip 的 

using (var stream = data.file.OpenReadStream())
using (var compressedFileStream = new FileStream($@"{root}\images\{guid}.zip", FileMode.Create))
using (var zipArchive = new ZipArchive(compressedFileStream, ZipArchiveMode.Update, false))
{
    var zipEntry = zipArchive.CreateEntry(data.file.FileName);
    using (var zipEntryStream = zipEntry.Open())
    {
        stream.CopyTo(zipEntryStream);
    }
}

core 支援 ZipArchive 哦

 

下載也很容易 

public UploadController(
        IHostingEnvironment environment,
        IContentTypeProvider contentTypeProvider
)
{
    this.environment = environment;
    this.contentTypeProvider = contentTypeProvider;
}

private IHostingEnvironment environment { get; set; }
private IContentTypeProvider contentTypeProvider { get; set; }
        
[HttpGet("download-file")]
public FileResult downloadFile(string name, string display)
{
    string contentType;
    contentTypeProvider.TryGetContentType(name, out contentType);
    HttpContext.Response.ContentType = contentType;
    string path = environment.WebRootPath + @"\images\" + name; // 注意哦, 不要像我這樣直接使用客戶端的值來拼接 path, 危險的 
    FileContentResult result = new FileContentResult(System.IO.File.ReadAllBytes(path), contentType)
    {
        FileDownloadName = display
    };
    return result;
}

html

<a href="/download-file?name=123.jpg&display=aaa.jpg" download  >download</a>

 

關鍵詞:file var new stream image environment public upload download files

相關推薦:

excel合併

Windows轉Linux總結(附帶常用Linux命令-LinuxMint)

文件完整性hash驗證demo(python指令碼)

Centos 7 關於shell、bash二三事,持續學習中

Java_IO程式設計

關於vtt 與 srt 字幕 的相互轉換

git的簡單使用入門

Python指令碼實現自動化Android手機apk安裝例項

InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,10]

python paramiko 遠端下載