Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to upload image to my local site folder, and my site is fully html based. The all response and request are mapped by wcf rest service so there is no server side pages used. I tried to upload image with "Scripting.FileSystemObject" but it is not working in mozilla firefox. So please, Suggest me what can i used for upload image which is working purely html and javascript.

Thanks,
Posted

There is no method to upload files using only HTML and JavaScript. These are both client-side and you need server-side processing of some type, .NET, PHP, etc. to upload files to a server.
 
Share this answer
 
Yes, there are no client side solution for image upload. Hence one can do it by calling the server side handler and do all the processes with handler.

I used handler in my code and used one of "Uploadify" named Control.

I also used two ".js" and one flash files which i have downloaded through

http://www.uploadify.com/download/

XML
<script src="Scripts/jquery.uploadify.v2.1.4.js" type="text/javascript"></script>
<script src="Scripts/swfobject.js" type="text/javascript"></script>



And after include above two files and one uploadify.swf call the uploadfiy function.

See the following code.

C#
$(document).ready(function () {
    $("#upImage").uploadify({
        'uploader': 'uploadify.swf',
        'cancelImg': './Images/cancel.png',
        'buttonText': 'Select Image',
        'script': 'Upload.ashx',
        'folder': '/Images',
        'fileDesc': 'Image Files',
        'fileExt': '*.jpg;*.jpeg;*.gif;*.png',
        'multi': false,
        'auto': true,
        'onComplete': function (event, ID, fileObj, response) {
            rootPath = fileObj.name;


        }
    });
}
);



I have taken html input file control named "upImage" by using this control, i have called the handler "Upload.ashx".

This is my handler code

C#
<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;

public class Handler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        if (context.Request.Files.Count > 0)
        {
            string path = context.Server.MapPath("Images/DealsImage");
            if (!System.IO.Directory.Exists(path))
                System.IO.Directory.CreateDirectory(path);

            var file = context.Request.Files[0];

            string fileName;

            if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE")
            {
                string[] files = file.FileName.Split(new char[] { '\\' });
                fileName = files[files.Length - 1];
            }
            else
            {
                fileName = file.FileName;
            }
            string strFileName = fileName;
            fileName = System.IO.Path.Combine(path, fileName);
            file.SaveAs(fileName);


            string msg = "{";
            msg += string.Format("error:'{0}',\n", string.Empty);
            msg += string.Format("msg:'{0}'\n", strFileName);
            msg += "}";
            context.Response.Write(msg);


        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900