zoukankan      html  css  js  c++  java
  • 文件写入文件分布式系统(asp.net C#)

     1 public JsonResult HandleImgUpload(User user, System.Web.HttpPostedFileBase fileData, long shopId = 0)
     2         {
     3              try
     4             {
     5                  System.Drawing.Image img = System.Drawing.Image.FromStream(fileData.InputStream);
     6                  if (img.Height <= 800 && img.Width <= 600)
     7                  {
     8                      // 设置参数
     9                      HttpWebRequest request = WebRequest.Create(Settings.GetSettingByKey("uploadUrl_img") + user.UserID) as HttpWebRequest;
    10                      CookieContainer cookieContainer = new CookieContainer();
    11                      request.CookieContainer = cookieContainer;
    12                      request.AllowAutoRedirect = true;
    13                      request.Method = "POST";
    14 
    15                      string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
    16                      request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
    17                      byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("
    --" + boundary + "
    ");
    18                      byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("
    --" + boundary + "--
    ");
    19                     
    20                      //请求头部信息 
    21                      StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name="file";filename="{0}"
    Content-Type:application/octet-stream
    
    ", fileData.FileName));
    22                      byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());
    23 
    24                      byte[] bytes = new byte[fileData.InputStream.Length];
    25                      fileData.InputStream.Seek(0, SeekOrigin.Begin);
    26                      fileData.InputStream.Read(bytes, 0, bytes.Length);
    27 
    28                      Stream postStream = request.GetRequestStream();
    29                      postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
    30                      postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
    31                      postStream.Write(bytes, 0, bytes.Length);
    32                      postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
    33                      postStream.Close();
    34                      //发送请求并获取相应回应数据
    35                      HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    36                      //直到request.GetResponse()程序才开始向目标网页发送Post请求
    37                      Stream instream = response.GetResponseStream();
    38                      StreamReader sr = new StreamReader(instream, Encoding.UTF8);
    39                      //返回结果网页(html)代码
    40                      string content = sr.ReadToEnd();
    41                      var info = Newtonsoft.Json.JsonConvert.DeserializeObject<UploadFileResult>(content);
    42 
    43                      if (info.Result == 0 && info.Info != null)
    44                      {
    45                          return Json(new { error = 0, url = info.Info.Url }, JsonRequestBehavior.AllowGet);
    46                      }
    47                      else
    48                      {
    49                          return Json(new { error = 1, message = info.Msg }, JsonRequestBehavior.AllowGet);
    50                      }
    51                  }
    52                  else 
    53                  {
    54                      return Json(new { error = 1, message = "文件尺寸超过限制(高:800,宽:600)" }, JsonRequestBehavior.AllowGet);
    55                  }
    56             }
    57             catch
    58             {
    59                 return Json(new { error = 1, message = "上传失败" }, JsonRequestBehavior.AllowGet);
    60             }
    61         }
    View Code
     1   class UploadFileResult
     2     {
     3         public int Result { set; get; }
     4         public int ErrorCode { set; get; }
     5         public string Msg { set; get; }
     6         public UploadFileResultInfo Info { set; get; }
     7     }
     8 
     9     class UploadFileResultInfo
    10     {
    11         public string FileName { set; get; }
    12         public string Surfix { set; get; }
    13         public string Url { set; get; }
    14         public long FileSize { set; get; }
    15 
    16     }
    View Code
  • 相关阅读:
    IOS Charles(代理服务器软件,可以用来拦截网络请求)
    Javascript中addEventListener和attachEvent的区别
    MVC中实现Area几种方法
    Entity Framework Code First 中使用 Fluent API 笔记。
    自定义JsonResult解决 序列化类型 System.Data.Entity.DynamicProxies 的对象时检测到循环引用
    序列化类型 System.Data.Entity.DynamicProxies 的对象时检测到循环引用
    An entity object cannot be referenced by multiple instances of IEntityChangeTracker 的解决方案
    Code First :使用Entity. Framework编程(8) ----转发 收藏
    Code First :使用Entity. Framework编程(6) ----转发 收藏
    Code First :使用Entity. Framework编程(5) ----转发 收藏
  • 原文地址:https://www.cnblogs.com/liaofeng/p/4666534.html
Copyright © 2011-2022 走看看