Quantcast
Channel: ASP.NET AJAX + Ajax Control Toolkit (ACT)
Viewing all articles
Browse latest Browse all 5678

Saving Uploaded Image To Folder

$
0
0

Hi all, I am attempting to create a C# version of the drag drop image uploader. Most of the tutorials I have seen on the subject were using  javascript xhr object and php. My problem is after sending the image to the server in the form of an xhr object, I'm not quite sure how to access that image file and save it to the folder of my choice. The following are snippets of javascript for sending an image file from the frontend to the server.

 function upload(file) {
                 xhrObj = new XMLHttpRequest;
                 xhrObj.open('post', 'DragDrop.aspx', true);
                 xhrObj.setRequestHeader('Content-Type', "multipart/form-Data");
                 xhrObj.setRequestHeader('X-File-Name', file.fileName);
                 xhrObj.setRequestHeader('X-File-Size', file.fileSize);
                 xhrObj.setRequestHeader('X-File-type', file.fileType);

                 xhrObj.send(file);
            };

The following is the C# code in the codebehind of DragDrop.aspx which I've tried to use to access the image file and save it to the folder of my choice in Visual Studio:

protected void Page_Load(object sender, EventArgs e)
        {
            Stream input = (Stream)Request.InputStream;

            Bitmap bmp = new Bitmap(input);
            Response.Clear();            
            
            switch(input.GetType().ToString())
            {
                case "jpeg":
                Response.ContentType = "image/jpeg";
                    break;
                case "gif":
                    Response.ContentType = "image/gif";
                    break;
                case "png":
                    Response.ContentType = "image/png";
                    break;
                default:
                    Response.ContentType = "application/octet-stream";
                    break;
            }

            Response.ContentType = "image/jpeg";
            bmp.Save("@'Upload\test1.jpeg'", ImageFormat.Jpeg);
        }

The code throws no error and nothing happens when I drag and drop my image into a DIV which had been designated as a droppable DIV on my webpage. I've already made sure that the javascript is working and that it's not the reason for the upload failure. Please take a look at my code and point out anything that I might be doing wrong. Thanks in advance for your help.


Viewing all articles
Browse latest Browse all 5678

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>