Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Upload progress bar

Posted on 2013-06-17
7
548 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 80

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

829 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