首页 > 编程语言 >C# HttpPostedFile传值,储存

C# HttpPostedFile传值,储存

时间:2023-12-19 19:44:26浏览次数:34  
标签:string C# httpFiles att file new 附件 HttpPostedFile 传值

HttpContext.Current.Request.Files附件保存

接口实现

     /// <summary>
        /// Post请求,Params传参
        /// </summary>
        /// <param name="url"></param>
        /// <param name="onHeading"></param>
        /// <returns></returns>
        private object PostMobileApiHost(string url, MultipartFormDataContent formData, Action<HttpRequestHeaders> onHeading = null)
        {
            using (HttpClient client = new HttpClient())
            {
                try
                {
                    VerHttps(url);

                    if (onHeading != null)
                    {
                        onHeading.Invoke(client.DefaultRequestHeaders);
                    }

                    HttpResponseMessage response = client.PostAsync(url, formData).Result;

                    if (response.IsSuccessStatusCode)
                    {
                        return response.Content.ReadAsStringAsync().Result;
                    }
                    else
                    {
                        return response.StatusCode;
                    }
                }
                catch
                {
                    throw;
                }
            }
        }

  接收附件,使用接口再次调用

         HttpFileCollection httpFiles = HttpContext.Current.Request.Files;
                if (httpFiles != null && httpFiles.Count > 0)
                {
                      HttpPostedFile file = httpFiles[0];
                        MultipartFormDataContent from = new MultipartFormDataContent();
                        //第一个附件,一次仅上传一个文件
                        byte[] fileBytes = new byte[file.ContentLength]; // 文件内容的字节数组  
                        file.InputStream.Read(fileBytes, 0, file.ContentLength);
                        ByteArrayContent fileContent = new ByteArrayContent(fileBytes);
                        var fileName = file.FileName;
                        fileContent.Headers.ContentType = new MediaTypeHeaderValue(file.ContentType);
                        fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                        {
                            Name = "file",
                            FileName = fileName
                        };
                        from.Add(fileContent, "file", fileName);
                        PostMobileApiHost("url", from, OnHeading);
                }

  存储附件

string path = "根路径";
                HttpFileCollection httpFiles = HttpContext.Current.Request.Files;
                List<自定义保存附件类> listAtt = new List<SysAttachment>();
                //保存附件
                if (httpFiles != null && httpFiles.Count > 0)
                {
                    if (string.IsNullOrEmpty(path))
                    {
                        throw new Exception("未设置物理路径地址!");
                    }
                    //附件分组名称集合
                    string[] keys = httpFiles.AllKeys;
                    
                    //保存附件列表
                    for (int i = 0; i < httpFiles.Count; i++)
                    {
                        var postFile = httpFiles[i];
                       
                        string extension = VerifyExtension(postFile.FileName);//验证后缀名
                        string newFileName = string.Format("{0}{1}", Guid.NewGuid().ToString(), extension);
                        string newFilePath = string.Format("{0}{1}", "储存路径", newFileName);
                        string savePath = string.Format("{0}{1}", path.TrimEnd('\\'), newFilePath.Replace("/", "\\"));

                        if (FileHelper.FolderExists(string.Format("{0}{1}", path.TrimEnd('\\'), UploadFolder), true))
                        {
                            //保存附件
                            postFile.SaveAs(savePath);
                            
                            SysAttachment att = new SysAttachment();
                            att.ParentId = 0;
                            att.FileName = postFile.FileName;
                            att.FilePath = newFilePath;
                            att.Extension = extension;
                            att.FileSize = postFile.ContentLength.ToString();
                            att.ContentType = postFile.ContentType;
                            listAtt.Add(att);
                        }
                    }
                }

  

 

标签:string,C#,httpFiles,att,file,new,附件,HttpPostedFile,传值
From: https://www.cnblogs.com/HoFei/p/17914533.html

相关文章

  • 2023最新中级难度Spring Security面试题,包含答案。刷题必备!记录一下。
    好记性不如烂笔头内容来自[面试宝典-中级难度SpringSecurity面试题合集](https://offer.houxu6.top/tag/SpringSecurity)问:如何在SpringBoot项目中集成SpringSecurity?为了在SpringBoot项目中集成SpringSecurity,需要遵循以下步骤:添加依赖在pom.xml文件中,添加Spr......
  • 《初学C语言第32天》
    //////——————————————————10.指针笔试题//////笔试题1//#include<stdio.h>//intmain()//{//  inta[5]={1,2,3,4,5};//  int*ptr=(int*)(&a+1);//  printf("%d,%d",*(a+1),*(ptr-1));//*(a+1):指的是数组a中第二个元素2......
  • CSP2023-12树上搜索题解
    刚考完csp,这道题是大模拟题,题意不难理解。以下是题目链接:http://118.190.20.162/view.page?gpid=T178当时考场上这道题调了好久没调出来,忽略了很多细节。在这里分享一下满分题解及思路,帮大家避避坑。#include<iostream>#include<stdio.h>#include<queue>#include<cstring>#inc......
  • AtCoder Beginner Contest 333
    B-Pentagon难度:⭐题目大意给定一个正五边形,其顶点为ABCDE;给定端点a,b,c,d;问a,b之间的距离和c,d之间的距离是否相等;解题思路两个端点之间的距离就看两个端点之间隔了几条边就行;并且因为是五边形,隔x条边和隔5-x条边是等价的;神秘代码#include<bits......
  • opencv图像处理机器学习真实项目教程(python实现)1计算机视觉简介
    1计算机视觉简介欢迎来到计算机视觉的世界。本书将带您踏上令人兴奋且快速发展的计算机视觉和图像处理世界的旅程。本书首先介绍计算机视觉和OpenCV库。然后,我们将继续介绍本课程的基本库和所需的环境设置。主要内容:计算机视觉简介计算机视觉的应用PythonOpenCVOpenCV......
  • 以 Git 为例演示 CentOS 系统安装软件的方式
    前言在Linux系统上安装软件有两种常用的方式。一种是通过包管理工具,直接安装;另一种是先下载源码,然后再手动编译,再安装。本文以Git为例,演示这两种安装软件的方式。使用yum安装Yum是CentOS的包管理工具。使用前先更新下Yum源:yumupdate使用包管理器安装软件非常简单,直接一......
  • 高等数值分析(高性能计算,并行计算) (Parallel and High Performance Computing)
    https://github.com/OpenMPhttps://math.ecnu.edu.cn/~jypan/Teaching/ParaComp/ParallelandHighPerformanceComputing(高等数值分析(高性能计算,并行计算))基本信息:教材:本课程主要讲授数值并行计算,内容以课堂讲义为主主要参考资料:并行计算与实现技术,迟学斌等,科学出版社,20......
  • 【Verilog】编码规范-coding sytle
    目前所在单位并没有代码规范文档,以致于阅读代码很吃力,并且久而久之自己写的代码可读性也没法保证。在参考了很多资料后,决定按以下规范来写:一、命名规范1、文件命名a、每个文件中只包含一个module、class、package,文件名于文件内容名称应相同。 2、module、class、package、f......
  • 任务调度处理系列之 Spring源码分析-【SchedulingConfigurer实现原理】转
     一、可能的场景在做业务平台的时候我们经常会遇到,某些跟时间打交道的需要修改状态,比如说在时间区间之前,属于未生效状态,区间之内属于有效期,区间之后,属于过期,或者需要每天每周每月,甚至是年为单位的做一些固定的操作。通过定时任务可以通过开启定时任务来完成这些需求。我做合......
  • tomcat随笔
    JDK的线程池,是先使用核心线程数配置,接着使用队列长度,最后再使用最大线程配置。Tomcat的线程池,就是先使用核心线程数配置,再使用最大线程配置,最后才使用队列长度核心10最大200队列 Integer.Max_Valueserver.tomcat.max-connections=10默认值是8192server.tomcat.accept-c......