Home>

The method includes the judgment of picture size limit, picture size, file content, and so on.. .

This case is a demo under mvc, which supports single image upload.

public actionresult upload ()
    {
      string imgurl="";
      foreach (string key in request.files)
      {
        //Only test uploading the first image file [0] here
        httppostedfilebase file0=request.files [key];
        //Convert to byte, read picture mime type
        stream stream;
        int size=file0.contentlength/1024;//file size kb
        if (size>1024)
        {
          return content (returnmsg (enum_return. failed, "Image cannot exceed 1m:", null));
        }
        byte [] filebyte=new byte [2];//contentlength, here we only read the first two bits of the file length for judgment,This is faster.
The rest are not used.
        stream=file0.inputstream;
       stream.read (filebyte, 0, 2);//contentlength, still take the first two bits
        //Get the picture width and height
        //system.drawing.image image=system.drawing.image.fromstream (stream);
        //int width=image.width;
        //int height=image.height;
        string fileflag="";
        if (filebyte!=null&filebyte.length>0) //The image data is empty
        {
          fileflag=filebyte [0] .tostring () filebyte [1] .tostring ();
        }
        string [] filetypestr={"255216", "7173", "6677", "13780"};//The corresponding image format jpg, gif, bmp, png
        if (filetypestr.contains (fileflag))
        {
          string action=request ["action"];
          string path="/uploads /";
          switch (action)
          {
            case "headimage":
              path="headimage /";
              break;
            case "blogtype":
              path="blogtype /";
              break;
          }
          string fullpath=path userinfo.userid "/";
          if (! directory.exists (server.mappath (fullpath)))
          {
            directory.createdirectory (server.mappath (fullpath));
          }
          request.files [key] .saveas (server.mappath (fullpath request.files [key] .filename));
          imgurl=fullpath request.files [key] .filename;
        }
        else
        {
          return content (returnmsg (enum_return. failed, "The image is malformed:" fileflag, null));
        }
        stream.close ();
      }
      return content (returnmsg (enum_return. success, "upload successful", imgurl));
    }

General processing procedure

public void processrequest (httpcontext context)
  {
    context.response.contenttype="application/json";
    httppostedfile _upfile=context.request.files ["file"];
    if (_upfile.contentlength<500000)
    {
      if (string.isnullorempty (_upfile.filename))
      {
         context.response.write ("Please upload an image");
      }
      string filefullname=_upfile.filename;
      string dataname=datetime.now.tostring ("yyyymmddhhmmss");
      string filename=filefullname.substring (filefullname.lastindexof ("\\") 1);
      string type=filefullname.substring (filefullname.lastindexof (".") 1);
      if (type == "bmp" || type == "jpg" || type == "gif" || type == "jpg" || type == "bmp" || type == "gif")
      {
        _upfile.saveas (httpcontext.current.server.mappath ("photo") "\\" dataname "." type);
        httpcookie cookie=new httpcookie ("photo");
        context.response.write ("Upload succeeded");
      }
      else
      {
        context.response.write ("Supported formats:| jpg | gif | bmp |");
      }
    }
    else
    {
      context.response.write ("Your picture is over 500k in size!");
    }
  }
c
  • Previous Android efficiently load large and multi-picture solutions to effectively avoid program OOM
  • Next C # assembly and reflection