android 和asp.net core笔记| 视频上传到指定目录 实时进度条

前端Android

上传事件

  private void startUpload(){
        String url = baseUrl + "/User/upload";
        MaterialDialog.Builder builder = new MaterialDialog.Builder(this)
                .title("进度")
                .content("上传中")
                .contentGravity(GravityEnum.CENTER)
                .progress(false, 100, true) //第一个参数要是是true那就是个圈圈转,没有进度
                .showListener(dialog -> {})
                .cancelable(false);
        MaterialDialog dialog = builder.show();
        RxHttp.postForm(url) //发送Form表单形式的Post请求
                .addFile("file", videoFile)
                .add("quote_item_no",quote_item_no)
                .add("rt_no",rt_no)
                .add("MovieCode",MovieCode)
                .upload(AndroidSchedulers.mainThread(), progress -> {
                    Log.d("progress", progress.getProgress()+"");
                    dialog.setProgress(progress.getProgress());
                    int currentProgress = progress.getProgress(); //当前进度 0-100
                    long currentSize = progress.getCurrentSize(); //当前已上传的字节大小
                    long totalSize = progress.getTotalSize();     //要上传的总字节大小
                    //上传进度回调,0-100,仅在进度有更新时才会回调
                })   //指定回调(进度/成功/失败)线程,不指定,默认在请求所在线程回调
                .asString()
                .timeout(99999, TimeUnit.SECONDS)
                .to(RxLife.toMain(this))
                .subscribe(data -> {
                            dialog.dismiss();
                            JSONObject jsonObject= (JSONObject) JSONObject.parse(data);
                            HelpSet.Dialog_event("上传成功\n"+jsonObject.getString("Message"), this);
                        }
                        , throwable -> {
                            dialog.dismiss();
                            HelpSet.Dialog_event("失败", this);
                        }
                );
    }

MaterialDialog和Rxhttp结合 可以显示进度条

依赖Rxhttp

  //后面都是Rxhttp的依赖
    implementation 'com.ljx.rxhttp:rxhttp:2.4.2'
    annotationProcessor 'com.ljx.rxhttp:rxhttp-compiler:2.4.2' //生成RxHttp类,纯Java项目,请使用annotationProcessor代替kapt
    implementation 'com.squareup.okhttp3:okhttp:4.9.1' //rxhttp v2.2.2版本起,需要手动依赖okhttp
    implementation 'com.ljx.rxlife:rxlife-coroutine:2.0.1' //管理协程生命周期,页面销毁,关闭请求

    //rxjava3
    implementation 'io.reactivex.rxjava3:rxjava:3.0.6'
    implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
    implementation 'com.ljx.rxlife3:rxlife-rxjava:3.0.0'
    compileOnly 'com.google.android.wearable:wearable:2.8.1'//管理RxJava3生命周期,页面销毁,关闭请求

后端Asp.net core

Controller

自定义路径需要配置IIS的选项

自定义路径必须符合 1.路径该服务器主机能访问到 2.IIS用户名配置正确

[RequestSizeLimit(int.MaxValue)]//设定请求大小为2GB左右(如需设置请求大小设置此特性)
[DisableRequestSizeLimit]
[HttpPost("upload")]

        public async Task<IActionResult> UploadVideo(IFormFile file, [FromForm] string quote_item_no, [FromForm] string rt_no,[FromForm] string MovieCode)
        {
            if (file == null || file.Length == 0)
            {
                return BadRequest("Invalid file");
            }
            var filePathSQL = Dbhelp.FindfilePath(); //这里的路径是主机能访问到的任何路径
            if (!filePathSQL.Equals("")&&!MovieCode.Equals(""))
            {           
                var fileName = MovieCode + Path.GetExtension(file.FileName);
                var filePath = Path.Combine(filePathSQL, fileName);
                // 确保目录存在,如果不存在则创建
                var directoryPath = Path.GetDirectoryName(filePath);
                if (!Directory.Exists(directoryPath))
                {
                    Directory.CreateDirectory(directoryPath);
                }
                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    await file.CopyToAsync(stream);
                }
                return Ok(new { Message = $"文件路径:{filePath}" , FilePath = filePath });
            }
            else
            {
                return NotFound(new { Message = "NotFound Path" });
            }
        }

记得头部请求一定要加

[RequestSizeLimit(int.MaxValue)]//设定请求大小为2GB左右(如需设置请求大小设置此特性)
[DisableRequestSizeLimit]

路径 

 var filePathSQL = Dbhelp.FindfilePath(); //这里的路径是主机能访问到的任何路径,包括局域网中主机能访问的共享文件夹

配置IIS

应用程序池中的高级设置本主机的用户名,密码是锁屏密码,看图

这样本机能访问的文件都可以访问到

文件过大会报错 IIS 10.0 Detailed Error - 413.1 - Request Entity Too Large 解决

在Startup.cs 文件

 public void ConfigureServices(IServiceCollection services)
{
  services.Configure<FormOptions>(options =>
            {
                options.MultipartBodyLengthLimit = int.MaxValue;
            });
            services.Configure<IISServerOptions>(options =>
            {
                options.MaxRequestBodySize = int.MaxValue;
            });
}

然后配置IIS中的这个

配置编辑器双击

搞定了自定义上传路径,不用在wwwroot下面了

要是需要在网站下面的wwwroot 的话就,就不需要配置这么多,直接

start.cs中开静态文件

   public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
   app.UseStaticFiles();//这句话是静态文件wwwroot
}

Controller


        [RequestSizeLimit(int.MaxValue)]//设定请求大小为2GB左右(如需设置请求大小设置此特性)
        [DisableRequestSizeLimit]
        [HttpPost("upload")]
 //存www本地目录
        public async Task<IActionResult> uploadideo(IFormFile file)
        {
            if (file == null || file.Length == 0)
            {
                return BadRequest("Invalid file");
            }

            var fileName = "1" + Path.GetExtension(file.FileName);

            // 构建 wwwroot 目录下的路径
            var filePath = Path.Combine(_hostEnvironment.WebRootPath, "uploads", fileName);

            // 确保目录存在,如果不存在则创建
            var directoryPath = Path.GetDirectoryName(filePath);
            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }

            return Ok(new { Message = "Upload successful", FilePath = filePath });
        }

搞定,完成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值