ashx image upload

In order to facilitate a multi-image upload method first call we will upload pictures pulled out method 

Create a new method ashx

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;

namespace TaoTong
{
    ///  <Summary> 
    /// Summary Description ImgUpLoad
     ///  </ Summary> 
    public  class ImgUpLoad: the IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World");
        }

        public  bool IsReusable
        {
            get
            {
                return false;
            }
        }
        ///  <the Summary> 
        /// pictures uploaded and saved to the server returns the relative path to save the image of the URL of
         ///  </ the Summary> 
        ///  <param name = "context"> </ param> 
        ///  <param name = "File"> </ param> 
        ///  <Returns> </ Returns> 
        public  String ImgUp (the HttpContext context)
        {
            // upload the first file 
            HttpPostedFile File = context.Request.Files [ 0 ];
             // get the image name 
            String imgName = file.FileName;
             // get the picture extension 
            String ImgExtention = System.IO.Path.GetExtension (imgName);
             // picture stream file is stored in the byte sequence container 
            stream stream = file.InputStream;
             // convert the image file to image image stream objects 
            image IMG = Image.FromStream (stream);

            // pictures stored on the server

            // To prevent duplicate image names we use random numbers named 
            Random RAN = new new Random (( int ) DateTime.Now.Ticks);
             // save the picture save directory by date 
            String subPath = " / imgUploads / " + DateTime.Now .ToString ( " yyyyMMdd " ) + " / " ; // 20,190,928
             // save the image directory absolute path 
            String path = context.Server.MapPath (subPath);
             // create a folder when the folder path does not exist 
            iF ( false == System.IO.Directory.Exists (path))
            {
                // Create a pic folder 
                System.IO.Directory.CreateDirectory (path);
            }
            String imgName = ran.Next ( 99999 ) + ImgExtention;
             String ServerPath = + imgName path; // file location and the name 
            String imgpath + = subPath imgName;
             the try
            {
                img.Save(serverPath);
                return imgPath;
            }
            catch
            {
                return "";
            }
        }

    }
}

 

Relative path has uploaded images image upload method to upload pictures to call the above method in which you have to use the methods described herein can be received in return

   
            ImgUpLoad load = new ImgUpLoad();
            string imgUrl = load.ImgUp(context);

 

Guess you like

Origin www.cnblogs.com/taikongbai/p/11770379.html