Solved

Upload progress bar

Posted on 2013-06-17
7
543 Views
Last Modified: 2013-06-30
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
0
Comment
Question by:ayha1999
  • 3
  • 3
7 Comments
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
ID: 39254351
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.
0
 
LVL 7

Author Comment

by:ayha1999
ID: 39260391
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
0
 
LVL 27

Expert Comment

by:Sammy
ID: 39265073
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.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 7

Author Comment

by:ayha1999
ID: 39265081
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
0
 
LVL 27

Accepted Solution

by:
Sammy earned 250 total points
ID: 39266488
Of course you can, its just a server control with its own events.
here is an example on Microsoft site with a step by step
http://support.microsoft.com/kb/323246
0
 
LVL 7

Author Comment

by:ayha1999
ID: 39266526
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
0
 
LVL 27

Expert Comment

by:Sammy
ID: 39269947
Do you actually want someone to do this for you and provide you with the code?
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now