zoukankan      html  css  js  c++  java
  • WebClient 上传文件

     MVC下 服务端代码:

     [HttpPost]
            public ActionResult UploadImg(string types)
            {string data = "";
                try
                {
                    if (types == "image")
                    {
                        foreach (string f in Request.Files.AllKeys)
                        {
                            string pathT = HttpRuntime.AppDomainAppPath.ToString() + "/UpLoadImages/";
                            string pathD = DateTime.Now.ToString("yyyyMMdd") + "/" + DateTime.Now.ToString("HHmm") + "/";
                            string sPath = pathT + pathD;
                            if (!Directory.Exists(sPath))
                            {
                                Directory.CreateDirectory(sPath);
                            }
                            HttpPostedFileBase file = Request.Files[f];
    
                            Random seed = new Random();
                            int randomNum = seed.Next(10, 99);
                            string fileName = DateTime.Now.ToString("HHmmss") + randomNum.ToString() + ".jpg";
                            file.SaveAs(sPath + fileName);
                            string ImgStr = sPath + fileName;
                            data = "{"Code":"10000","Message":"" + ImgStr + ""}";
    
                        }
                    }
                    else
                    {
                        data = "{"Code":"-10000","Message":"上传格式不正确"}";
                    }
                }
                catch (Exception ex)
                {
                    data = "{"Code":"-10000","Message":"" + ex.Message + ""}";
                }
    
    
                return Content(data, "application/json");
            }

    客户端代码: UpLoadImage.aspx
       public partial class UpLoadImage : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                WebClient webclient = new WebClient();
                string fileName = @"C:UsersadminDesktop	om2.jpg";
              byte[] responseArray = webclient.UploadFile("http://localhost:8987/Home/UploadImg?types=image", "POST", fileName);
    
                string getPath = Encoding.GetEncoding("UTF-8").GetString(responseArray);
                Response.Write(getPath);
            }
        }
  • 相关阅读:
    浴谷夏令营2017.8.1数论的整理
    BZOJ1483: [HNOI2009]梦幻布丁
    NOIP2014-11-3模拟赛
    BZOJ3884: 上帝与集合的正确用法
    BZOJ4869: [Shoi2017]相逢是问候
    计蒜客NOIP2017提高组模拟赛(三)day1
    NOIP2014-9-6模拟赛
    NOIP2014-7-7模拟赛
    zoj Little Keng(快速幂)
    多校Key Set (快速幂)
  • 原文地址:https://www.cnblogs.com/dragon-L/p/4720958.html
Copyright © 2011-2022 走看看