tnblog
首页
视频
资源
登录

文件上传

3720人阅读 2020/3/31 15:39 总访问:437474 评论:0 收藏:0 手机
分类: ASP.NET
  1. [HttpPost]
  2.         public dynamic UploadSaveOSS()
  3.         {
  4.             try
  5.             {
  6.                 //int timeStamp = System.Web.HttpContext.Current.Request.QueryString["ts"].ToInt32(0);
  7.                 //超出有效期
  8.                 //if (Math.Abs((int)DateTime.Now.ToUnixTimeStamp() - timeStamp) > Const.PcAccessValidSeconds)
  9.                 //{
  10.                 //    throw new HttpResponseException(HttpStatusCode.Unauthorized);
  11.                 //}
  12.                 long userId = 0;
  13.                 //string token = HttpContext.Current.Request.Form["token"];
  14.                 //if (string.IsNullOrEmpty(token))
  15.                 //{
  16.                 //    return CommonService.GetErrorResult(Const.ParamErrorTip);
  17.                 //}
  18.                 //JwtBody jwtBody = CommonHelper.GetUserInfoFromAuth2<JwtBody>("Bearer " + token);
  19.                 //if (jwtBody.nameid < 1)
  20.                 //{
  21.                 //    return CommonService.GetErrorResult(Const.FailRetryTip);
  22.                 //}
  23.                 //userId = jwtBody.nameid;
  24.                 HttpFileCollection files = HttpContext.Current.Request.Files;
  25.                 HttpPostedFile postedFile = files["file"];
  26.                 if (postedFile.ContentLength > CommonHelper.ConvertToByte(Const.UploadMaxLength))
  27.                 {
  28.                     return CommonService.GetErrorResult("文件大小不能超过" + Const.UploadMaxLength);
  29.                 }
  30.                 int maxlength = 5242880;//5M=5*1024*1024
  31.                 string size = HttpContext.Current.Request.Form["size"];
  32.                 if (!string.IsNullOrEmpty(size))
  33.                 {
  34.                     maxlength = CommonHelper.ConvertToByte(size);
  35.                 }
  36.                 if (postedFile.ContentLength > maxlength)
  37.                 {
  38.                     return CommonService.GetErrorResult("文件大小不能超过" + size);
  39.                 }
  40.                 else
  41.                 {
  42.                     int utp = Convert.ToInt32(HttpContext.Current.Request.Form["utp"]);
  43.                     string fileName, fileExtension;
  44.                     fileName = System.IO.Path.GetFileName(postedFile.FileName);
  45.                     if (fileName != "")
  46.                     {
  47.                         string newpath = System.DateTime.Now.ToString("yyyyMMdd");
  48.                         string savepath = Const.WebFileFolderName + "/temp/" + newpath + "/";
  49.                         if (utp == (sbyte)UploadType.KnowledgePic)
  50.                         {
  51.                             savepath = Const.WebFileFolderName + "/KnowledgePic/";
  52.                         }
  53.                         fileExtension = System.IO.Path.GetExtension(fileName).ToLower();
  54.                         string filename = System.DateTime.Now.ToString("yyyyMMddhhmmssfff") + fileExtension;
  55.                        
  56.                         OssClient client = new OssClient(Const.EndPoint, Const.AccessKeyId, Const.AccessKeySecret);
  57.                         string key = savepath + filename;
  58.                         client.PutObject(Const.WebFileBucketName, key, postedFile.InputStream);
  59.                         //获取访问url
  60.                         // 设置URL过期时间为50年,图片最好设置为1天过期,源文件1小时过期,图片获取的时候循环判断是否过期,源文件下载的时候判断过期
  61.                         DateTime expiration = DateTime.Now.AddYears(50);
  62.                         Uri uri = client.GeneratePresignedUri(Const.WebFileBucketName, key, expiration);
  63.                         string path = uri.ToString();
  64.                         int affect = 1;
  65.                         
  66.                         if (affect > 0)
  67.                         {
  68.                             return CommonService.GetResult("", ErrorCodeType.Success, new
  69.                             {
  70.                                 path = path,
  71.                                 name = fileName
  72.                             });
  73.                         }
  74.                         else
  75.                         {
  76.                             return CommonService.GetErrorResult("上传失败");
  77.                         }
  78.                     }
  79.                     else
  80.                     {
  81.                         return CommonService.GetErrorResult("上传文件为空");
  82.                     }
  83.                 }
  84.             }
  85.             catch
  86.             {
  87.                 return CommonService.GetErrorResult(Const.FailRetryTip);
  88.             }
  89.         }


评价

ASP.NET MVC + WebUploader+BootStrap实现文件上传进度条显示

引入需要的资源文件:&lt;scriptsrc=&quot;~/Content/jquery-1.9.1.min.js&quot;&gt;&lt;/script&gt; &lt;linkhref=&quot;...

ASP.NET MVC + WebUploader实现文件上传二:使用中的一些问题

如何把文件选择按钮换成自己的因为WebUploader设置关联的上传id会有他的样式,有时候想换成自己的开始想的是添加自己的文件...

.net core上传文件大文件问题文件上传超长,文件太大

1. Request body too large 错误Post的body大概超过30m就会报这个错误,完整的错误是Microsoft.AspNetCore.Server.Kestrel....

nginx限制文件上传大小

有些时候用了nginx,除了项目本身要配置上传大小限制,nginx也要限制 直接贴代码吧 location / { proxy_set_header X...

文件上传选择单类型和多类型上传(可上传音乐,视频,图片,文档)

HTML:```handlebars &lt;linkhref=&quot;~/Content/layui/css/layui.css&quot;rel=&quot;stylesheet&quot;/&gt; &lt;fie...

文件上传具体操作(txtpptpngjpgexexlsxlsxpptx.mp3/.wav/.mid/.asf/.mpg/.avi/.tti,avi、 rm 、 等)

```handlebarshtml代码: &lt;linkhref=&quot;~/Content/layui/css/layui.css&quot;rel=&quot;stylesheet&quot;/&gt; &lt...

XAF配置多文件上传

在bo文件创建两个类主要配置主从关系默认继承BaseObject将其修改为FileAttachmentBase[DefaultClassOptions] public class...

Web Uploader文件上传组件文档,文件分段上传,进度条显示

引入资源使用Web Uploader文件上传需要引入三种资源:JS, CSS, SWF。&lt;!--引入CSS--&gt; &lt;linkrel=&quot;stylesheet&...

实用技术-文件上传 01

一、1、MultipartResolver接口和文件上传下载坐标坐标2、案例、结构环境1、pom.xml&lt;!--servlet规范--&gt; &lt;dependen...

vue elementui,vue3 element plus 文件上传的时候设置其他参数。后台.net接收传递的额外参数。图片上传

比如上传文件的时候额外传递两个select选择的值 前台前面上传文件的时候要提供默认参数很简单,el-upload绑定一个data即可...

jsjquery得到文件上传的名称获取上传文件的文件名

html: &lt;input type=&quot;file&quot; name=&quot;file&quot; id=&quot;choiseFile&quot; accept=&quot;.zip,.rar&quot;...

input 文件上传类型限制。上传图片预览,获取上传文件名称,获取上传文件的后缀名,限制文件大小等

限制只能上传图片image/* &lt;input type=&quot;file&quot; name=&quot;file&quot; id=&quot;choiseFile&quot; accept=&qu...

.net6.net core 文件上传封装。枚举使用。封装通用的文件上传

封装的代码如下using Microsoft.AspNetCore.Http; using System; using System.Collections.Generic; using System.IO; ...

使用minio 实现分布式文件上传

前言由于项目业务发展,文件上传直接保存到服务器的弊端越来越多。后面采用了分布式的文件上传,因为是内网的,所以上网找...
当你知道迷惑时,并不可怜,当你不知道迷惑时,才是最可怜的。
排名
11
文章
201
粉丝
10
评论
13
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术