Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Upload progress bar

Posted on 2013-06-17
7
Medium Priority
?
559 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
[X]
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
  • 3
  • 3
7 Comments
 
LVL 83

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 Ageil
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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 Ageil earned 1000 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 Ageil
ID: 39269947
Do you actually want someone to do this for you and provide you with the code?
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…
Suggested Courses

598 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