zoukankan      html  css  js  c++  java
  • C# 通过http post 请求上传图片和参数

    一、C# Winform或控制台

     1 /// <summary>
     2         /// 通过http上传图片及传参数
     3         /// </summary>
     4         /// <param name="imgPath">图片地址(绝对路径:D:demoimg123.jpg)</param>
     5         public void UploadImage(string imgPath)
     6         {
     7             var uploadUrl = "http://localhost:3020/upload/imgup";
     8             var dic = new Dictionary<string, string>() {
     9                     {"para1",1.ToString() },
    10                     {"para2",2.ToString() },
    11                     {"para3",3.ToString() },
    12                 };
    13             var postData = Utils.BuildQuery(dic);//转换成:para1=1&para2=2&para3=3
    14             var postUrl = string.Format("{0}?{1}", uploadUrl, postData);//拼接url
    15             HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
    16             request.AllowAutoRedirect = true;
    17             request.Method = "POST";
    18 
    19             string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
    20             request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
    21             byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("
    --" + boundary + "
    ");
    22             byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("
    --" + boundary + "--
    ");
    23 
    24             int pos = imgPath.LastIndexOf("\");
    25             string fileName = imgPath.Substring(pos + 1);
    26 
    27             //请求头部信息 
    28             StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name="file";filename="{0}"
    Content-Type:application/octet-stream
    
    ", fileName));
    29             byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());
    30 
    31             FileStream fs = new FileStream(imgPath, FileMode.Open, FileAccess.Read);
    32             byte[] bArr = new byte[fs.Length];
    33             fs.Read(bArr, 0, bArr.Length);
    34             fs.Close();
    35 
    36             Stream postStream = request.GetRequestStream();
    37             postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
    38             postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
    39             postStream.Write(bArr, 0, bArr.Length);
    40             postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
    41             postStream.Close();
    42 
    43             HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    44             Stream instream = response.GetResponseStream();
    45             StreamReader sr = new StreamReader(instream, Encoding.UTF8);
    46             string content = sr.ReadToEnd();
    47         }

    二、图片接收接口

    [ValidateInput(false)]
            public JsonResult imgup(string para1,string para2,string para3)
            {
                if (Request.Files.Count > 0)
                {
                    //传过来的图片
                    var file = Request.Files[0];
                    //保存到本地或服务器
    
                }
                return new JsonResult { };
            }
     1  /// <summary>
     2         /// 上传图片信息
     3         /// </summary> 
     4         /// <returns></returns>
     5         [ValidateInput(false)]
     6         public JsonResult ImgUp()
     7         {
     8             try
     9             {
    10                 if (Request.Files.Count > 0)
    11                 {
    12                     HttpPostedFileBase file = Request.Files[0];
    13                     string path = file.FileName;//获取Execle文件名    
    14                     string IsXls = System.IO.Path.GetExtension(path).ToString().ToLower();
    15                     string fileName = DateTime.Now.ToString("yyyyMMddhh") + IsXls;
    16                     string savePath = Server.MapPath(("~\Upload\") + fileName);//Server.MapPath 获得虚拟服务器相对路径
    17                     file.SaveAs(savePath); //SaveAs 将上传的文件内容保存在服务器上 
    18                 }
    19                 return Json(new { Success = true, Message = "成功" }, JsonRequestBehavior.AllowGet);
    20             }
    21             catch (Exception ex)
    22             {
    23                 return Json(new { Success = false, Message = "失败" }, JsonRequestBehavior.AllowGet);
    24             }
    25         }

     转自连接:https://www.cnblogs.com/pingming/p/8550802.html

    认真工作、认真生活,努力做最好的自己!!!
  • 相关阅读:
    C语言 sprintf 函数 C语言零基础入门教程
    C语言 printf 函数 C语言零基础入门教程
    C语言 文件读写 fgets 函数 C语言零基础入门教程
    C语言 文件读写 fputs 函数 C语言零基础入门教程
    C语言 fprintf 函数 C语言零基础入门教程
    C语言 文件读写 fgetc 函数 C语言零基础入门教程
    C语言 文件读写 fputc 函数 C语言零基础入门教程
    C语言 strlen 函数 C语言零基础入门教程
    Brad Abrams关于Naming Conventions的演讲中涉及到的生词集解
    适配器模式
  • 原文地址:https://www.cnblogs.com/songhuihui/p/14628475.html
Copyright © 2011-2022 走看看