c++上传oss中文路径报错_C# 从Oss对象存储器上下载文件到本地打包下载代码
生活随笔
收集整理的這篇文章主要介紹了
c++上传oss中文路径报错_C# 从Oss对象存储器上下载文件到本地打包下载代码
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
[HttpGet][Route("downloadimgszip")]public void downloadimages(string goodsid){//獲取所有圖片路徑List<string> arraylist = GetAllImagePathsByGoodId(goodsid);var DownPicpath = System.Web.HttpContext.Current.Server.MapPath("/DownPicPackge");//獲取IIS服務(wù)器臨時(shí)文件目錄 //創(chuàng)建文件夾 if (!Directory.Exists(DownPicpath))Directory.CreateDirectory(DownPicpath);var siteconfig = _configCache.GetConfigSiteGlobal();foreach (var item in arraylist){//從oss服務(wù)器上下載圖片文件到IIS服務(wù)器臨時(shí)目錄文件夾var client = new OssClient(siteconfig.remote_oss_host, siteconfig.remote_oss_accesskey, siteconfig.remote_oss_accesssecret);// 下載文件到流。OssObject 包含了文件的各種信息,如文件所在的存儲(chǔ)空間、文件名、元信息以及一個(gè)輸入流。//這里的item需要注意格式,例如:1233/123.jpg, 路徑第一個(gè)字符前要是有斜線的一定要去掉,否則會(huì)報(bào)錯(cuò)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;// 通過輸入流將文件的內(nèi)容讀取到文件或者內(nèi)存中。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();//異步刪除壓縮前,下載的臨時(shí)文件Task.Run(() =>{if (Directory.Exists(curDirName))Directory.Delete(curDirName, true);});}/// <summary>/// 創(chuàng)建壓縮文件/// </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); // 壓縮級(jí)別 0-9//s.Password = "123"; //Zip壓縮文件密碼byte[] buffer = new byte[4096]; //緩沖區(qū)大小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();}}
如果代碼對(duì)您有幫助,幫忙打個(gè)賞,支持原創(chuàng),感謝老鐵們的支持。。
總結(jié)
以上是生活随笔為你收集整理的c++上传oss中文路径报错_C# 从Oss对象存储器上下载文件到本地打包下载代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 从键盘上录入10科考试分数,输出最高分最
- 下一篇: 互联网三大基石