Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
1 Star 0 Fork 319

TWT20161022/视频学习+在线考试+题库+直播

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (4)
Tags (8)
master
dev
beta
optimize
2.3
2.1.1.21041
2.0.8627
2.0.8499
2.0.8460
2.0.8384
2.0.8355
2.0Beta
master
Branches (4)
Tags (8)
master
dev
beta
optimize
2.3
2.1.1.21041
2.0.8627
2.0.8499
2.0.8460
2.0.8384
2.0.8355
2.0Beta
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (4)
Tags (8)
master
dev
beta
optimize
2.3
2.1.1.21041
2.0.8627
2.0.8499
2.0.8460
2.0.8384
2.0.8355
2.0Beta
Upload.cs 14.63 KB
Copy Edit Raw Blame History
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Xml;
using Newtonsoft.Json.Linq;
using System.IO;
using Song.Entities;
using Song.ServiceInterfaces;
using Song.ViewData.Attri;
using WeiSha.Core;
namespace Song.ViewData.Methods
{
/// <summary>
/// 上传文件管理,包括普通上传、大文件分上传、上传资源查看
/// </summary>
[HttpPut, HttpGet]
public class Upload : ViewMethod, IViewAPI
{
/// <summary>
/// 获取指定文件夹下的文件信息
/// </summary>
/// <param name="pathkey">路径配置项,来自web.config中的upload节点</param>
/// <param name="dataid">数据id,为了保持资源的存放与数据对应,例如试题编辑时,试题相关联的图片等存放在试题id为命名的文件下,方便删除试题时一起删除相关资源</param>
/// <returns></returns>
public new JArray Files(string pathkey, long dataid)
{
JArray arr = new JArray();
if (string.IsNullOrWhiteSpace(pathkey)) return arr;
string virPath = WeiSha.Core.Upload.Get[pathkey].Virtual + (dataid > 0 ? dataid.ToString() + "/" : "");
string phyPath = WeiSha.Core.Upload.Get[pathkey].Physics + (dataid > 0 ? dataid.ToString() : "");
if (!Directory.Exists(phyPath)) return arr;
DirectoryInfo dir = new DirectoryInfo(phyPath);
foreach (FileInfo f in dir.GetFiles())
{
JObject jo = new JObject();
jo.Add("name", f.Name);
jo.Add("path", virPath);
jo.Add("full", virPath + f.Name);
jo.Add("ext", string.IsNullOrWhiteSpace(f.Extension) ? "" : f.Extension.Replace(".", ""));
jo.Add("length", f.Length.ToString());
jo.Add("size", _filesize_convert(f.Length));
arr.Add(jo);
}
return arr;
}
/// <summary>
/// 图片资源
/// </summary>
/// <param name="pathkey">路径配置项,来自web.config中的upload节点</param>
/// <param name="dataid">数据id,</param>
/// <returns></returns>
public JArray Images(string pathkey, long dataid)
{
JArray arr = new JArray();
if (string.IsNullOrWhiteSpace(pathkey)) return arr;
string virPath = WeiSha.Core.Upload.Get[pathkey].Virtual + (dataid > 0 ? dataid.ToString() + "/" : "");
string phyPath = WeiSha.Core.Upload.Get[pathkey].Physics + (dataid > 0 ? dataid.ToString() : "");
if (!Directory.Exists(phyPath)) return arr;
DirectoryInfo dir = new DirectoryInfo(phyPath);
string[] exts = "jpg,jpeg,gif,png,bmp".Split(',');
List<FileInfo> files = new List<FileInfo>();
foreach (FileInfo f in dir.GetFiles())
{
string abbr = f.Name.IndexOf(".") > -1 ? f.Name.Substring(0, f.Name.LastIndexOf(".")) : f.Name;
if (abbr.EndsWith("_small")) continue;
foreach (string ext in exts)
{
if (f.Extension.ToLower() == "." + ext)
{
files.Add(f);
break;
}
}
}
foreach (FileInfo f in files)
{
JObject jo = new JObject();
jo.Add("name", f.Name);
jo.Add("path", virPath);
jo.Add("full", virPath + f.Name);
jo.Add("ext", string.IsNullOrWhiteSpace(f.Extension) ? "" : f.Extension.Replace(".", ""));
jo.Add("length", f.Length.ToString());
jo.Add("size", _filesize_convert(f.Length));
//图片格式
try
{
using (System.Drawing.Image image = System.Drawing.Image.FromFile(f.FullName))
{
jo.Add("width", image.Width);
jo.Add("height", image.Height);
}
}
catch { }
arr.Add(jo);
}
return arr;
}
private string _filesize_convert(long length)
{
int KB = 1024;
int MB = KB * 1024;
int GB = MB * 1024;
if (length / GB >= 1)
return Math.Round(length / (double)GB, 2) + " GB";
else if (length / MB >= 1)
return Math.Round(length / (float)MB, 2) + " MB";
else if (length / KB >= 1)
return Math.Round(length / (float)KB, 2) + " KB";
else
return length + " Byte";
}
/// <summary>
/// 上传图片
/// </summary>
/// <param name="pathkey"></param>
/// <param name="dataid"></param>
/// <param name="small">是否要生成缩略图,为true时生成</param>
/// <param name="swidth">缩略图的宽度</param>
/// <param name="sheight">缩略图的高度</param>
/// <returns></returns>
[Admin, Teacher]
[HttpPost, HttpGet(Ignore = true)]
[Upload(Extension = "jpg,png,gif", MaxSize = 1024, CannotEmpty = true)]
public JObject ImageSave(string pathkey, long dataid, bool small,int swidth,int sheight)
{
if (base.Files.Count < 1) return null;
if (string.IsNullOrWhiteSpace(pathkey)) return null;
string virPath = WeiSha.Core.Upload.Get[pathkey].Virtual + (dataid > 0 ? dataid.ToString() + "/" : "");
string phyPath = WeiSha.Core.Upload.Get[pathkey].Physics + (dataid > 0 ? dataid.ToString() + "/" : "");
if (!Directory.Exists(phyPath)) Directory.CreateDirectory(phyPath);
string filename = string.Empty, smallfile = string.Empty;
//只保存第一张图片
foreach (string key in base.Files)
{
HttpPostedFileBase file = base.Files[key];
filename = WeiSha.Core.Request.UniqueID() + Path.GetExtension(file.FileName);
file.SaveAs(phyPath + filename);
//生成缩略图
if (small)
{
smallfile = WeiSha.Core.Images.Name.ToSmall(filename);
WeiSha.Core.Images.FileTo.Thumbnail(phyPath + filename, phyPath + smallfile, swidth, sheight, 0);
}
break;
}
JObject jo = new JObject();
System.IO.FileInfo f = new FileInfo(phyPath + filename);
jo.Add("name", f.Name);
jo.Add("path", virPath);
jo.Add("full", virPath + f.Name);
jo.Add("ext", string.IsNullOrWhiteSpace(f.Extension) ? "" : f.Extension.Replace(".", ""));
jo.Add("length", f.Length.ToString());
jo.Add("size", _filesize_convert(f.Length));
using (System.Drawing.Image image = System.Drawing.Image.FromFile(f.FullName))
{
jo.Add("width", image.Width);
jo.Add("height", image.Height);
}
if (small) jo.Add("small", smallfile);
return jo;
}
/// <summary>
/// 上传图片
/// </summary>
/// <param name="url">图片地址</param>
/// <param name="pathkey"></param>
/// <param name="dataid"></param>
/// <param name="small">是否要生成缩略图,为true时生成</param>
/// <param name="swidth">缩略图的宽度</param>
/// <param name="sheight">缩略图的高度</param>
/// <returns></returns>
[Admin, Teacher]
[HttpPost, HttpGet(Ignore = true)]
public JObject ImageLoad(string url,string pathkey, long dataid, bool small, int swidth, int sheight)
{
if (string.IsNullOrWhiteSpace(pathkey)) return null;
string virPath = WeiSha.Core.Upload.Get[pathkey].Virtual + (dataid > 0 ? dataid.ToString() + "/" : "");
string phyPath = WeiSha.Core.Upload.Get[pathkey].Physics + (dataid > 0 ? dataid.ToString() + "/" : "");
if (!Directory.Exists(phyPath)) Directory.CreateDirectory(phyPath);
string filename = string.Empty, smallfile = string.Empty;
filename = WeiSha.Core.Request.UniqueID() + Path.GetExtension(url);
try
{
WeiSha.Core.Request.LoadFile(url, phyPath + filename);
//生成缩略图
if (small)
{
smallfile = WeiSha.Core.Images.Name.ToSmall(filename);
WeiSha.Core.Images.FileTo.Thumbnail(phyPath + filename, phyPath + smallfile, swidth, sheight, 0);
}
JObject jo = new JObject();
System.IO.FileInfo f = new FileInfo(phyPath + filename);
jo.Add("name", f.Name);
jo.Add("path", virPath);
jo.Add("full", virPath + f.Name);
jo.Add("ext", string.IsNullOrWhiteSpace(f.Extension) ? "" : f.Extension.Replace(".", ""));
jo.Add("length", f.Length.ToString());
jo.Add("size", _filesize_convert(f.Length));
using (System.Drawing.Image image = System.Drawing.Image.FromFile(f.FullName))
{
jo.Add("width", image.Width);
jo.Add("height", image.Height);
}
if (small) jo.Add("small", smallfile);
return jo;
}
catch(Exception ex)
{
throw ex;
}
}
/// <summary>
/// 删除文件
/// </summary>
/// <param name="file">文件所在完整虚拟路径</param>
/// <returns></returns>
[Admin, Teacher]
[HttpDelete, HttpGet(Ignore = true)]
public bool Delete(string file)
{
string phy = this.Context.Server.MapPath(file);
if (File.Exists(phy))
File.Delete(phy);
else
throw new Exception("文件不存在");
return true;
}
#region 大文件上传
private static readonly object __chunkedMerge_lock = new object();
/// <summary>
/// 分片上传文件
/// </summary>
/// <param name="pathkey">文件存放的路径,来自web.config中upload节点的key</param>
/// <param name="filename">文件名</param>
/// <param name="total">总片数</param>
/// <param name="index">当前分片的索引</param>
/// <param name="uid">唯一值,用于分片的标识</param>
/// <returns></returns>
[HttpPost]
[Upload(CannotEmpty = true, MaxSize = int.MaxValue)]
[Admin, Teacher]
public JObject Chunked(string pathkey, string filename, int total, int index, string uid)
{
//资源的虚拟路径和物理路径
string tempHy = WeiSha.Core.Upload.Get["Temp"].Physics;
//文件保存
foreach (string key in this.Letter.Files)
{
HttpPostedFileBase file = this.Letter.Files[key];
string chunkname = Path.Combine(tempHy, index + "." + uid);
file.SaveAs(chunkname);
break;
}
JObject jo = new JObject();
jo.Add("pathkey", pathkey);
jo.Add("name", filename);
jo.Add("total", total);
jo.Add("index", index);
jo.Add("uid", uid);
//上传完成的数量
int complete = System.IO.Directory.GetFiles(tempHy, "*." + uid).Length;
jo.Add("completed", complete);
if (complete == total)
{
//最终合并后的文件
string final = Path.Combine(tempHy, filename);
lock (__chunkedMerge_lock)
{
try
{
using (FileStream fs = new FileStream(final, FileMode.Create))
{
for (int i = 1; i <= total; i++)
{
string part = Path.Combine(tempHy, i + "." + uid);
if (System.IO.File.Exists(part))
{
byte[] bytes = System.IO.File.ReadAllBytes(part);
fs.Write(bytes, 0, bytes.Length);
bytes = null;
System.IO.File.Delete(part);
}
}
fs.Close();
}
}
catch (System.IO.IOException ex)
{
WeiSha.Core.Log.Error(this.GetType().FullName, ex);
}
}
//移动文件到指定文件夹
try
{
string folder = WeiSha.Core.Upload.Get[pathkey].Physics;
string finalfile = WeiSha.Core.Upload.NameFilter(filename); //最终要存储的文件名
int serial = 1;
while (System.IO.File.Exists(Path.Combine(folder, finalfile)))
{
string ext = Path.GetExtension(filename);
string prefix = filename.IndexOf(".") > -1 ? filename.Substring(0, filename.LastIndexOf(".")) : filename;
finalfile = prefix + "_" + (serial++) + ext;
}
//new System.Threading.Tasks.Task(() =>
//{
//await System.Threading.Tasks.Task.Delay(500);
//System.Threading.Thread.Sleep(2000);
System.IO.File.Move(final, Path.Combine(folder, finalfile));
//}).Start();
jo.Add("success", true);
jo.Add("filename", finalfile);
jo.Add("ext", Path.GetExtension(filename).Replace(".", ""));
jo.Add("path", WeiSha.Core.Upload.Get[pathkey].Virtual + finalfile);
}
catch (Exception ex)
{
WeiSha.Core.Log.Error(this.GetType().FullName, ex);
throw ex;
}
}
else
{
jo.Add("success", false);
jo.Add("filepath", "");
}
return jo;
}
#endregion
}
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

前后端分离,C#、Vue。web端采用ElementUI,手机端采用VantUI,管理后台采用WebdeskUI。直播、视频学习、试题练习、测试、考试、学习证明、成绩打印,实现"学、练、考"一体。
Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/wtt2016/LearningSystem.git
git@gitee.com:wtt2016/LearningSystem.git
wtt2016
LearningSystem
视频学习+在线考试+题库+直播
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /