日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

c++上传oss中文路径报错_C# 从Oss对象存储器上下载文件到本地打包下载代码

發布時間:2025/3/12 C# 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++上传oss中文路径报错_C# 从Oss对象存储器上下载文件到本地打包下载代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
[HttpGet][Route("downloadimgszip")]public void downloadimages(string goodsid){//獲取所有圖片路徑List<string> arraylist = GetAllImagePathsByGoodId(goodsid);var DownPicpath = System.Web.HttpContext.Current.Server.MapPath("/DownPicPackge");//獲取IIS服務器臨時文件目錄 //創建文件夾 if (!Directory.Exists(DownPicpath))Directory.CreateDirectory(DownPicpath);var siteconfig = _configCache.GetConfigSiteGlobal();foreach (var item in arraylist){//從oss服務器上下載圖片文件到IIS服務器臨時目錄文件夾var client = new OssClient(siteconfig.remote_oss_host, siteconfig.remote_oss_accesskey, siteconfig.remote_oss_accesssecret);// 下載文件到流。OssObject 包含了文件的各種信息,如文件所在的存儲空間、文件名、元信息以及一個輸入流。//這里的item需要注意格式,例如:1233/123.jpg, 路徑第一個字符前要是有斜線的一定要去掉,否則會報錯var obj = client.GetObject(siteconfig.remote_oss_bucketname, item);var downloadFilename = Path.GetFileName(item);using (var requestStream = obj.Content){byte[] buf = new byte[1024];var fs = File.Open(DownPicpath + "/" + downloadFilename, FileMode.OpenOrCreate);var len = 0;// 通過輸入流將文件的內容讀取到文件或者內存中。while ((len = requestStream.Read(buf, 0, 1024)) != 0){fs.Write(buf, 0, len);}fs.Close();}}//聲明壓縮文件名string zipName = DateTime.Now.ToString("yyyyMMddhhmmss") + ".zip";if (File.Exists(zipName)){File.Delete(zipName);}CreateZipFile(DownPicpath, DownPicpath + "/" + zipName);//通知瀏覽器下載文件而不是打開 FileStream fsstream = new FileStream(DownPicpath + "/" + zipName, FileMode.Open);byte[] bytes = new byte[(int)fsstream.Length];fsstream.Read(bytes, 0, bytes.Length);fsstream.Close();System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";//通知瀏覽器下載文件而不是打開 System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(zipName, System.Text.Encoding.UTF8));System.Web.HttpContext.Current.Response.BinaryWrite(bytes);System.Web.HttpContext.Current.Response.Flush();System.Web.HttpContext.Current.Response.End();//異步刪除壓縮前,下載的臨時文件Task.Run(() =>{if (Directory.Exists(curDirName))Directory.Delete(curDirName, true);});}/// <summary>/// 創建壓縮文件/// </summary>/// <param name="filesPath"></param>/// <param name="zipFilePath"></param>private static void CreateZipFile(string filesPath, string zipFilePath){ if (!Directory.Exists(filesPath)){Console.WriteLine("Cannot find directory '{0}'", filesPath);return;} string[] filenames = Directory.GetFiles(filesPath);using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath))){s.SetLevel(9); // 壓縮級別 0-9//s.Password = "123"; //Zip壓縮文件密碼byte[] buffer = new byte[4096]; //緩沖區大小foreach (string file in filenames){ZipEntry entry = new ZipEntry(Path.GetFileName(file));entry.DateTime = DateTime.Now;s.PutNextEntry(entry);using (FileStream fs = File.OpenRead(file)){int sourceBytes;do{sourceBytes = fs.Read(buffer, 0, buffer.Length);s.Write(buffer, 0, sourceBytes);} while (sourceBytes > 0);}}s.Finish();s.Close();}}

如果代碼對您有幫助,幫忙打個賞,支持原創,感謝老鐵們的支持。。

總結

以上是生活随笔為你收集整理的c++上传oss中文路径报错_C# 从Oss对象存储器上下载文件到本地打包下载代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。