上传到Azure中需要引用

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Drawing;
using System.IO;
using System.Web;namespace WEBAPI.Common
{public class FileBll{private static string _blobKey =@"DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1};EndpointSuffix=core.windows.net";//Blob Key#region 上传到本地/// <summary>/// Excel文件導出目錄/// </summary>public const string ExcelAbsolutePath = "\\Temp\\";/// <summary>/// 獲取Excel導出文件夾路徑/// </summary>/// <returns>Excel導出文件夾路徑</returns>public static string GetExcelExportFolderPath(){return ConfigHelper.GetAppSetting("RootPath") + ExcelAbsolutePath;}/// <summary>/// 獲取加上時間后的文件名/// </summary>/// <param name="name">名稱</param>/// <param name="extension">後綴</param>/// <returns>文件名</returns>public static string GetFileNameAppendDataTime(string name, string extension){return string.Concat(name, DateTime.Now.ToString("yyyyMMdd_HHmmss"), ".", extension);}/// <summary>/// 獲取隨機文件名/// </summary>/// <param name="extension">文件後綴</param>/// <returns>隨機文件名</returns>public static string GetRandomFileName(string extension){return string.Concat(Guid.NewGuid().ToString().Replace("-", ""), ".", extension);}/// <summary>/// 将Base64字符串转换为Image对象/// </summary>/// <param name="base64Str">base64字符串</param>/// <returns></returns>public static Bitmap Base64StrToImage(string base64Str){Bitmap bitmap = null;try{byte[] arr = Convert.FromBase64String(base64Str);MemoryStream ms = new MemoryStream(arr);Bitmap bmp = new Bitmap(ms);ms.Close();bitmap = bmp;}catch (Exception ex){LogManager2.InfoFormat("保存图片失败FromBase64String:{0}", ex.Message);}return bitmap;}/// <summary>/// 将Base64字符串转换为图片并保存到本地/// </summary>/// <param name="base64Str">字符串的第一部分"data:image/png;base64" 是代表该Base64字符串对应的原始类型,第二部分是该文件生成的内容</param>/// <param name="savePath">图片保存地址,如:/Content/Images/10000.png</param>/// <returns></returns>public static string Base64StrToImage(string base64Str, string suffix, string savePath){var fileName = "";try{string[] img_array = base64Str.Split(',');var bitmap = Base64StrToImage(img_array[1]);if (bitmap != null){//这里复制一份对图像进行保存,否则会出现“GDI+ 中发生一般性错误”的错误提示var bmpNew = new Bitmap(bitmap);//判断文件夹是否存在,不存在新建if (!Directory.Exists(savePath)){Directory.CreateDirectory(savePath);}fileName = Guid.NewGuid() + "." + suffix;bmpNew.Save(savePath + "\\" + fileName);bmpNew.Dispose();bitmap.Dispose();}}catch (Exception ex){LogManager2.InfoFormat("保存图片失败:{0}", ex.Message);}return fileName;}#endregion 上传到本地#region 上传到微软云private string GetRandomString(){var dateNowStr = $"{DateTime.Now:yyyyMMddHHmmssfff}";Random rnd = new Random(Guid.NewGuid().GetHashCode());return $"{dateNowStr}{rnd.Next(0, 100).ToString().PadLeft(2, '0')}";}public void DeleteFile(string fileName){DelBlob(fileName);//var filePath = $"{HttpContext.Current.Server.MapPath("~/")}Upload\\{fileName}";//if (System.IO.File.Exists(filePath))//{//    System.IO.File.Delete(filePath);//}}/// <summary>///上传到微软云上面/// </summary>/// <param name="base64Str">文件编码</param>/// <param name="suffix">后缀名 jpg</param>/// <param name="savePath">保存地址</param>/// <param name="azureAccountKey">AzureKey</param>/// <param name="azureAccountName">AzureName</param>/// <returns></returns>public static string UploadAzureFile(string base64Str, string suffix, string savePath, string azureAccountKey, string azureAccountName){var fileName = Guid.NewGuid() + "." + suffix;string[] img_array = base64Str.Split(',');byte[] byteArray = Convert.FromBase64String(img_array[1]);//HTTP Context to get access to the submitted dataHttpContext postedContext = HttpContext.Current;//File Collection that was submitted with posted data//Make sure a file was postedstring contentType = "application/octet-stream";Stream stream = new MemoryStream(byteArray);_blobKey = string.Format(_blobKey,azureAccountName, azureAccountKey);//Blob KeyCloudStorageAccount storageAccount = CloudStorageAccount.Parse(_blobKey);// Create the blob clientCloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();// Retrieve a reference to a containerCloudBlobContainer container = blobClient.GetContainerReference(savePath);// Retrieve reference to a blob named "myblob"CloudBlockBlob blob = container.GetBlockBlobReference(fileName);// Create or overwrite the "myblob" blob with contents from a local fileblob.Properties.ContentType = contentType;blob.UploadFromStream(stream);return fileName;}public void DelBlob(string fileName){//CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_blobKey);Create the blob client//CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();Retrieve a reference to a container//CloudBlobContainer container = blobClient.GetContainerReference("upload");Retrieve reference to a blob named "myblob"//CloudBlockBlob blob = container.GetBlockBlobReference(fileName);//if (blob.Exists())//    blob.DeleteAsync();}private bool Exists(CloudBlob blob){try{blob.FetchAttributes();return true;}catch (Exception e){return false;}}public byte[] ReadToEnd(System.IO.Stream stream){long originalPosition = 0;if (stream.CanSeek){originalPosition = stream.Position;stream.Position = 0;}try{byte[] readBuffer = new byte[4096];int totalBytesRead = 0;int bytesRead;while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0){totalBytesRead += bytesRead;if (totalBytesRead == readBuffer.Length){int nextByte = stream.ReadByte();if (nextByte != -1){byte[] temp = new byte[readBuffer.Length * 2];Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);readBuffer = temp;totalBytesRead++;}}}byte[] buffer = readBuffer;if (readBuffer.Length != totalBytesRead){buffer = new byte[totalBytesRead];Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);}return buffer;}finally{if (stream.CanSeek){stream.Position = originalPosition;}}}#endregion 上传到微软云}
}

上传文件到本地操作和上传到Azure云上相关推荐

  1. 电脑和云服务器之间怎么传文件,Windows本地环境和Linux腾讯云服务器之间传输文件的方法...

    通过WinSCPWinSCP简介: WinSCP是一个Windows环境下使用SSH的开源图形化SFTP客户端.同时支持SCP协议.它的主要功能就是在本地与远程计算机间安全的复制文件. 与使用FTP上 ...

  2. JavaScript 获取上传文件的本地绝对路径

    一直苦恼于在表单提交时获得上传文件的本地绝对路径. 由于javascript是在浏览器环境运行的脚本语言,所以javascript的权限很低,不能操作本地资源,这样的好处是安全性提高了,但是也带来了开 ...

  3. IE浏览器上传文件时本地路径变成”C:\fakepath\”的问题

    IE浏览器上传文件时本地路径变成"C:\fakepath\"的问题 博客分类: javascript HTML 在使用<input id="file_upl&quo ...

  4. linux怎样自动检查link文件_自动共享和上传文件到兼容的托管站点 | Linux 中国

    Anypaste 将会根据你想上传的文件的类型来自动挑选合适的托管站点.简单地说,照片将被上传到图像托管站点,视频被传到视频站点,代码被传到 pastebin. -- Sk(作者) 前阵子我们写了一个 ...

  5. js并发上传文件到不同服务器,simple-uploader.js 功能强大的上传组件 - 文章教程

    simple-uploader.js(也称 Uploader)是一个上传库,支持多并发上传,文件夹.拖拽.可暂停继续.秒传.分块上传.出错自动重传.手工重传.进度.剩余时间.上传速度等特性:该上传库依 ...

  6. Fiddler小技巧-测试上传文件接口多参数并传情况

    写了多年的API了,fidder还真是方便至极相对于postman来说. 两种常用方式: 抓包:app通过代理方式,就可以在pc端看到fidder的请求了 因为会监控好多跟我们需要的没关系的HTTP请 ...

  7. 关于前端上传文件到后台,文件大小超出限制,导致上传失败的解决办法。

    文章出处:Jiangkuobo,微信:jkb_267460 文章目录 前言 一.使用elementPlus上传图片,出现的问题. 二.验证猜想 1.验证方式: 2.测试结果: 3.我的猜想与验证 三. ...

  8. 云服务器上传文件怎么这么慢,百度云上传速度慢怎么办?教你如何加快百度云上传速度...

    现在有很多朋友使用百度网盘进行资料存储,但是上传和下载的速度却总是有些问题.最近有位朋友的百度网盘下载速度很快,但是上传速度很慢的时候,不知道为什么会这样.那么,这种情况应该如何解决呢?下面就来看一下 ...

  9. mongodb python 存文件_Python保存MongoDB上的文件到本地的方法介绍

    本文实例讲述了Python保存MongoDB上的文件到本地的方法.分享给大家供大家参考,具体如下: MongoDB上的文档通过GridFS来操作,Python也可以通过pymongo连接MongoDB ...

  10. Android连接SQLServer详细教程(数据库+服务器+客户端),并在微软Azure云上搭建云服务

    Android连接SQLServer详细教程(数据库+服务器+客户端),并在微软Azure云上搭建云服务 参考博客:http://blog.csdn.net/zhyl8157121/article/d ...

最新文章

  1. h5打开App的方法。
  2. Edge Linking
  3. 用javascript做删除时的提示信息
  4. bzoj 2251: [2010Beijing Wc]外星联络
  5. HDU 5919 Sequence II 主席树
  6. leetcode 718. Maximum Length of Repeated Subarray | 718. 最长重复子数组(动态规划)
  7. Java实现一个字符串的反转
  8. hihocoder234周 计算不包含黑点的矩形个数
  9. 多个注解可以合并成一个,包括自定义注解
  10. html站点根目录怎么改,通过重定向把子目录设置为网站根目录
  11. Mysql数据库入门 (基础知识点 由来 各种指令 如何运用)
  12. 工程部署: 低算力平台模型性能的优化
  13. Kubernetes访问报错: No route to host
  14. 如何破解加密ppt文档的密码
  15. 最小公共子串——力扣
  16. 科腾Joystick手柄连接ROS
  17. TensorFlow Eager 教程
  18. Tesra超算网络,解决AI开发困境
  19. python as f是什么意思_Python中 with open(file_abs,'r') as f: 的用法以及意义
  20. 【计算机毕业设计】1.房屋租赁系统

热门文章

  1. Docker - 分布式任务调度中心 - xxl-job
  2. 计算机国际期刊等级,计算机期刊等级(全)
  3. Linux之Redhat7如何破解密码
  4. think-queue使用教程-用户注册场景异步发送邮件
  5. 金财助手 - 安装分析(一)
  6. P1957 口算练习题[c++版]
  7. 信息安全 —— 密码学
  8. 60秒倒计时实现的两种方式
  9. 企业erp系统对企业预算和采购环节的作用
  10. MySQL使用存储过程造数据