Link to home
Start Free TrialLog in
Avatar of ayha1999
ayha1999

asked on

Upload progress bar

Hi,

Can replace an .ashx file with c# class file? The following script shows file upload progress bar by linking to an .ashx file. I want to upload file by browsing the file only without clicking any button to upload. Please help me to modify the code or give me any sample if any.

aspx page:
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="AutoUpload.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
     <link rel="Stylesheet" type="text/css" href="CSS/uploadify.css" />
     <script type="text/javascript" src="scripts/jquery-1.3.2.min.js"></script>
    <script type="text/javascript" src="scripts/jquery.uploadify.js"></script>
</head>
<body>
<form id="form1" runat="server">
    <div style = "padding:40px">
        <asp:FileUpload ID="FuSTL" runat="server" />
    </div>
</form>
</body>
</html>
<script type = "text/javascript">
$(window).load(
    function() {
       $("#<%=FuSTL.ClientID %>").fileUpload({
        'uploader': 'scripts/uploader.swf',
        'cancelImg': 'images/cancel.png',
        'buttonText': 'Browse Files',
        'script': 'Upload.ashx',
        'folder': 'uploads',
        'fileDesc': 'STL Files',
        'fileExt': '*.stl',
        'multi': true,
        'auto': true
    });
   }
);
</script> 

Open in new window


.ashx file:
<%@ WebHandler Language="C#" Class="Upload" %>

using System;
using System.Web;
using System.IO;

public class Upload : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        context.Response.Expires = -1;
        try
        {
            HttpPostedFile postedFile = context.Request.Files["Filedata"];
            
            string savepath = "";
            string tempPath = "";
            tempPath = System.Configuration.ConfigurationManager.AppSettings["FolderPath"]; 
            savepath = context.Server.MapPath(tempPath);
            string filename = postedFile.FileName;
            if (!Directory.Exists(savepath))
                Directory.CreateDirectory(savepath);

            postedFile.SaveAs(savepath + @"\" + filename);
            context.Response.Write(tempPath + "/" + filename);
            context.Response.StatusCode = 200;
        }
        catch (Exception ex)
        {
            context.Response.Write("Error: " + ex.Message);
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
}

Open in new window



Thanks

ayha
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

How will your calling program know that you've completed browsing to the file and selected the right one?  It does need the user to indicate to the calling program that they have selected the correct file. This is especially the case in a web project, in which the user has control and not the caller.
Avatar of ayha1999
ayha1999

ASKER

Can I replace .ahsx file with classs file?  The code for writing the file to a folder is in .ahsx file. I want to do it in a button click event of aspx page. the javascript is referring to .ahsx file name. Can I still refer to .ahsx if I compile the project?

ayha
You cannot upload files without using an upload button. Users have to choose to upload files to your server. This is a security issue and no browser will allow you to do that.
I have to use upload button. Can I replace .ashx file with class file? I want write the code to upload file to folder and db in an aspx file instead of .ashx file.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Sammy
Sammy
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What I am trying here, to replace .ashx file with class file. the .ashx file referred in the    'script': 'Upload.ashx'," I want to remove it do all the upload operations in aspx code behind file.

Thanks
Do you actually want someone to do this for you and provide you with the code?