C # image upload problem

upload_xz function (Imgfile) { 
    ajaxLoading (); 
    Debugger; 
    var allFile = imgfile.files;
     var imageArr = [];
     var K = 0 ;
     for ( var I = 0 ; I <allFile.length; I ++ ) { 
        
        var File = allFile [I];
         // add a layer of filter 
        var Rfilter = / ^ (Image \ / BMP | Image \ / GIF | Image \ / JPEG | Image \ / PNG | Image \ / TIFF) $ / I;
         IF (! Rfilter. the Test (file.type)) { 
            Alert ( " file format must be an image " );
            return ; 
        } 
        var Reader = new new the FileReader (); 
        reader.readAsDataURL (File); // with the file loader loads the file 
        reader.onload = function ( Event ) { 
            Alert ( Event .target.result);
             var imgdata = Event .target .Result;
             // save to pass a background picture 
            $ .ajax ({ 
                of the type: " POST " , 
                url: " ../huanjing/HJHelp.ashx " , 
                the Data: { " Action": "uploadimg", "imgdata": imgdata, "imgpath": moreimgpath },
                error: function (ex) {
                    console.log("保存第" + (k + 1) + "张图片失败");
                    k++;
                },
                success: function (data) {
                    if (data != "-1") {
                        if (k < allFile.length + 1) {
                            debugger;
                            //返回的地址记录到span上
                            var filepaths = $("#upload_name_xz").text();
                            if (filepaths != "") {
                                $("#upload_name_xz").text(filepaths + "," + data);
                            } else {
                                $("#upload_name_xz").text(data);
                            }

                            console.log("Save the first " + (k + 1 ) + " pictures success " ); 
                            $ ( " #xzpic_count " ) .text ( " [ " + (k + 1 ) + " ] " ); 
                            k ++ ; 
                        } 
                        IF (k == allFile.length) { 
                            console.log ( " image upload complete " ); 
                            $ ( " #xzpic_count " ) .text ( "【upload completed】" ); 
                            
                        } 
                    } The else { 
                        Alert ( " check or replace the section " + (K + . 1 ) + " pictures " ); 
                        $ ( " #xzpic_count " ) .text ( " [] failed to upload " ); 
                    } 

                    ajaxLoadEnd () ; 
                } 
            }); 
        }; 
       
    } 
}

Distal Note: For the object image can be read filereader as base64 string and submit the background; Note here that, in the reading result filereader onload callback object, rather than readAsDataURL

 public  void SaveIMG (the HttpContext context) 
        { 
            String imgpath context.Request = [ " imgpath " ];
             String Base64 context.Request = [ " imgdata " ];
             // determines a picture type 
            String [] = IMGArr base64.Split ( ' ; ' ) ;
             String type = IMGArr [ 0 ] .split ( ' / ' ) [ . 1 ]; 

            // save the picture 
            String imgstr base64.Split = ( ' , ' ) [ . 1]; // pure image data
             // the System.Drawing.Bitmap Bitmap = null; // define a Bitmap object, complete conversion of the received images 
            the try 
            { 
                byte [] = ARR Convert.FromBase64String (imgstr); // Neat resources Base64 is converted to an equivalent 8-bit unsigned integer array 

                System.IO.MemoryStream MS = new new System.IO.MemoryStream (ARR); // converted into the MemoryStream not resize 
                the System.Drawing.Bitmap BMP = new new the System.Drawing .Bitmap (MS); // convert the object into Bitmap object MemoryStream 
                ms.Close (); // close the current flow, and releases all resources associated with 


                int width = bmp.Width;
                int height = bmp.Height;
                using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(bmp, width, height))
                {

                    string savepath = context.Server.MapPath(imgpath) + DateTime.Now.ToString("yyyyMMdd");
                    if (!System.IO.Directory.Exists(savepath))
                    {
                        System.IO.Directory.CreateDirectory(savepath);
                    }
                    string filename = DateTime.Now.ToString("yyyyMMddHHmmss_ffff");

                    bitmap.Save(savepath + "\\" + filename + "." + type);


                    context.Response.Write("/UploadFile/MoreImg/" + DateTime.Now.ToString("yyyyMMdd") + "/" + filename + "." + type);

                }
                
            }
            catch (Exception ex)
            {
                context.Response.Write("-1");
            }
        }

Back-end processing using C #, where attention to the problem using the life cycle of use and bitmap.

I did not use the original code

using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap (
 bmp, width, height)) 
lead, a bit depth of 24 images can not be saved, can only hold 32 bit depth images
repeatedly find reasons, has not been found Finally, look at the information appears GDI + generic error of 3 in the case, they would understand there is a problem here, after modification, the picture can be properly uploaded.

Guess you like

Origin www.cnblogs.com/sguozeng/p/12424184.html