• c# http文件上传


            /// <summary>
            /// 上传文件的api
            /// </summary>
            [HttpPost]
            public string UploadFile(op_client_billfile_info model)
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + "BillFile";
                path += model.path;
                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);
                model.filename = ExistFile(path, model.filename.Replace(" ", ""));
    
                MemoryStream ms = new MemoryStream(model.by);
                FileStream fs = new FileStream(path + "\" + model.filename, FileMode.OpenOrCreate);
                ms.WriteTo(fs);
                ms.Close();
                fs.Close();
                return model.filename;
            }
            /// <summary>
            /// 文件名重复加(1)
            /// </summary>
            [NonAction]
            private string ExistFile(string path, string filename)
            {
                int count = 1;
                //在重复名称后加(序号)
                while (File.Exists(path + "\" + filename))
                {
                    if (filename.Contains(")."))
                    {
                        int start = filename.LastIndexOf("(");
                        int end = filename.LastIndexOf(").") - filename.LastIndexOf("(") + 2;
                        filename = filename.Replace(filename.Substring(start, end), string.Format("({0}).", count));
                    }
                    else
                    {
                        filename = filename.Replace(".", string.Format("({0}).", count));
                    }
                    count++;
                }
                return filename;
            }

    上传文件类

            /// <summary>
            /// 账单文件信息的id
            /// </summary>        
            public int bid { get; set; }
            /// <summary>
            /// 文件名
            /// </summary>        
            public string filename { get; set; }
            /// <summary>
            /// 放在服务器的路径
            /// </summary>        
            public string path { get; set; }
            /// <summary>
            /// 文件
            /// </summary>
            public byte[] by { get; set; }

    修改上传文件大小限制,不然会报错。第一个文件的单位是 kb  也就是100M;第二个文件的单位是 byte  也是100M。

    <configuration>
      <system.web>
        <httpRuntime maxRequestLength="102400" executionTimeout="200" enable="true" />
      </system.web>
      <system.webServer>
        <security>
          <requestFiltering>
            <requestLimits maxAllowedContentLength="104857600" />
          </requestFiltering>
        </security>
      </system.webServer>
    </configuration>

    下载文件

            /// <summary>
            /// 保存文件
    /// url 文件地址(iis);path 保存地址;fileName 保存文件名
    /// </summary> private void DownloadFile(string url, string path, string fileName) { Stream sm = WebRequest.Create(url).GetResponse().GetResponseStream(); FileStream fs = new FileStream(path + "\" + fileName, FileMode.OpenOrCreate); sm.CopyTo(fs); sm.Close(); fs.Close(); }

     post请求封装地址:https://www.cnblogs.com/shuaimeng/p/9871582.html

  • 相关阅读:
    Power Designer如何批量改动数据类型
    javaWeb中URLEncoder.encode空格问题
    android不同机型上界面适配问题
    Linux命令之编辑
    Android fragment 切换载入数据卡顿问题
    oracle中设置了最大链接数还是报错
    [Erlang]Erlang经常使用工具解说
    云计算设计模式(二十三)——Throttling节流模式
    iOS 8 Share Extension Safari URL Example(在iOS中分享url的样例)
    CentOS下配置HTTPS訪问主机并绑定訪问port号
  • 原文地址:https://www.cnblogs.com/shuaimeng/p/10749212.html
Copyright © 2020-2023  润新知