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

    <!--enctype="multipart/form-data":如果要上传文件必须加上该属性,指定相应的编码。只有这样用户选择的文件数据(文件流)才会放在请求报文中,发送给服务器。表单中的其它表单元素(文本框等),也会发送到服务端,但是格式也变了,但是在服务端还是按照以前的方式进行接收-->
        <!--如果表单不需要上传文件就不用加enctype--->
        <form method="post" action="ProcessFileUp.ashx" enctype="multipart/form-data">
            <input type="file" name="fileUp"/>
            <input type="text" name="txtName" />
    
            <input type="submit" value="上传" />
    
        </form>

     ashx

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.IO;
    namespace CZBK.ItcastProject.WebApp._2015_5_27
    {
        /// <summary>
        /// ProcessFileUp 的摘要说明
        /// </summary>
        public class ProcessFileUp : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/html";
               HttpPostedFile file=context.Request.Files[0];//获取上传的文件.
               if (file.ContentLength>0)
               {
                   //对上传的文件类型进行校验。
                   string fileName =Path.GetFileName(file.FileName);//获取上传文件的名称包含扩展名。
                   string fileExt = Path.GetExtension(fileName);//获取用户上传的文件扩展名。
                   if (fileExt == ".jpg")
                   {
                       //1.对上传文件进行重命名?
                       string newfileName = Guid.NewGuid().ToString();
                       //2:将上传的文件放在不同的目录下面?
                       string dir = "/ImageUpload/"+DateTime.Now.Year+"/"+DateTime.Now.Month+"/"+DateTime.Now.Day+"/";
                       //创建文件夹
                       if (!Directory.Exists(context.Request.MapPath(dir)))
                       {
                           Directory.CreateDirectory(context.Request.MapPath(dir));
                       }
    
                       string fullDir = dir + newfileName + fileExt;//文件存放的完整路径。
                       file.SaveAs(context.Request.MapPath(fullDir));
    
                     //  file.SaveAs(context.Request.MapPath("/ImageUpload/"+fileName));//完成文件的保存。
                       context.Response.Write("<html><body><img src='"+fullDir+"'/></body></html>");
                   }
                   else
                   {
                       context.Response.Write("只能上传图片文件");
                   }
               }
               else
               {
                   context.Response.Write("请选择上传文件");
               }
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }
    View Code
        protected void Page_Load(object sender, EventArgs e)
        {
    
            HttpPostedFile jpeg_image_upload = Request.Files["Filedata"];
    
    
            string imageId =DateTime.Now.Ticks.ToString();
            string fileName = Path.GetFileName(jpeg_image_upload.FileName);
            string fileExtension = Path.GetExtension(fileName);
            string uploadName = imageId + fileExtension;
    
    
            if (jpeg_image_upload.ContentLength > 0)
            {
    
                string imageUrl = Server.MapPath("/UpLoadPic/Product/" + DateTime.Now.ToString("yyyyMM") + "/" + uploadName);
                if (!Directory.Exists(Path.GetDirectoryName(imageUrl)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(imageUrl));
                }
                jpeg_image_upload.SaveAs(imageUrl);
    
                //保存大图并水印
                string bigimg = Server.MapPath("/UpLoadPic/Product/" + DateTime.Now.ToString("yyyyMM") + "/c" + uploadName);
                if (!Directory.Exists(Path.GetDirectoryName(bigimg)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(bigimg));
                }
                NCurrtImg.GreateMiniImage(imageUrl, bigimg, 600, 600, "COM");//生成缩略图
                //CurrtImg.addWaterMark(tempimg, bigimg, "I");//水印
    
    
                //保存中图
                string midimg = Server.MapPath("/UpLoadPic/Product/" + DateTime.Now.ToString("yyyyMM") + "/m" + uploadName);
                if (!Directory.Exists(Path.GetDirectoryName(midimg)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(midimg));
                }
                NCurrtImg.GreateMiniImage(imageUrl, midimg, 200, 200, "COM");//生成缩略图
    
                //保存小图
                string smallimg = Server.MapPath("/UpLoadPic/Product/" + DateTime.Now.ToString("yyyyMM") + "/s" + uploadName);
                if (!Directory.Exists(Path.GetDirectoryName(smallimg)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(smallimg));
                }
                NCurrtImg.GreateMiniImage(imageUrl, smallimg, 33, 33, "COM");//生成缩略图
    
                Response.StatusCode = 200;
                string returnStr = jpeg_image_upload.ContentLength.ToString();//0 size
                returnStr = returnStr + "|" + "/UpLoadPic/Product/" + DateTime.Now.ToString("yyyyMM") + "/m" + uploadName;//1 url
                returnStr = returnStr + "|" + fileName; //2 title
                returnStr = returnStr + "|" + imageId; //3 id
                returnStr = returnStr + "|" + "wokaokao"; //4 name
                Response.Write(returnStr);
                Response.End();
            }
        }
        public void UpImg(JTZK_ProductModel Model)
        {
            HttpPostedFile file = Request.Files["PicUrl"];
            if (file == null)
            {
                // return ...
            }
            else
            {
                string fileName = System.IO.Path.GetFileName(file.FileName);
                string fileExtension = System.IO.Path.GetExtension(fileName);
                string uploadName = Guid.NewGuid() + fileExtension;
                string dir = "/UpLoadPic/Product/" + DateTime.Now.ToString("yyyyMM");
    
                string imgpath = dir + "/c" + uploadName;
                Model.PicUrl = imgpath;
                System.IO.Directory.CreateDirectory(Server.MapPath(dir));//创建文件夹.
                imgpath = Server.MapPath(imgpath);
                file.SaveAs(imgpath);
    
    
                string smallimg = "";
                string midimg = "";
                smallimg = Server.MapPath(dir + "/s" + uploadName);
                NCurrtImg.GreateMiniImage(imgpath, smallimg, 100, 100, "Cut");
    
                midimg = Server.MapPath(dir + "/m" + uploadName);
                NCurrtImg.GreateMiniImage(imgpath, midimg, 300, 300, "Cut");
            }
    
        }

    如果失败  考虑权限问题 和路径问题   在 saveAs前一定要创建路径

    #region 文件上传
            public ActionResult FileUpload()
            {
                HttpPostedFileBase postFile = Request.Files["fileUp"];//接收文件数据
                if (postFile == null)
                {
                    return Content("no:请选择上传文件");
                }
                else
                {
                    string fileName = Path.GetFileName(postFile.FileName);//文件名称
                    string fileExt = Path.GetExtension(fileName);//文件的扩展名称.
                    if (fileExt == ".jpg")
                    {
                        string dir = "/ImagePath/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/";
                        Directory.CreateDirectory(Path.GetDirectoryName(Request.MapPath(dir)));//创建文件夹.
                        string newfileName = Guid.NewGuid().ToString();//新的文件名称.
                        string fullDir = dir + newfileName + fileExt;//完整的路径.
                        postFile.SaveAs(Request.MapPath(fullDir));//保存文件.
                        return Content("ok:" + fullDir);
    
                    }
                    else
                    {
                        return Content("no:文件格式错误!!");
                    }
    
                }
            }
            #endregion
  • 相关阅读:
    x264参数
    用X264编码以后的H264数据
    (转)YUV420存储格式
    YUV数据YUY2到I420
    udp编程中,一次能发送多少个bytes为好?
    (转)c++多态实现的机制
    linux下ping加时间戳实时输出到文件 放后台运行
    如何向AcmeAir注入问题代码
    AcmeAir
    Jmeter压力测试
  • 原文地址:https://www.cnblogs.com/xiaoshi657/p/4707814.html
Copyright © 2011-2022 走看看