ASP.NET C# code to replace upload.php (upload.ashx)

Post samples of your implementations, code snippets & examples here.

ASP.NET C# code to replace upload.php (upload.ashx)

Postby TravisN. » Sun Mar 15, 2009 8:23 am

John Dyer submitted this snippet in the old forum

Here's some ASP.NET C# code to replace upload.php (upload.ashx)

Code: Select all
using System;
using System.Web;
using System.IO;

public class Upload : IHttpHandler
{

   public void ProcessRequest(HttpContext context)
   {         
      HttpPostedFile file = context.Request.Files["Filedata"];   
   
      string targetDirectory = Path.Combine(context.Request.PhysicalApplicationPath, context.Request["folder"].Replace("/",""));
      string targetFilePath = Path.Combine(targetDirectory, file.FileName);      
         
      // Uncomment the following line if you want to make the directory if it doesn't exist
      //if (!Directory.Exists(targetDirectory)) Directory.CreateDirectory(targetDirectory);

      file.SaveAs(targetFilePath);

      context.Response.Write("1");
   }

   public bool IsReusable
   {
      get { return false;   }
   }
}
Uploadify - where simplify and upload meet

Travis Nickels
Uploadify Developer
TravisN.
 
Posts: 656
Joined: Sat Mar 07, 2009 12:34 am
Location: Australia

Re: ASP.NET C# code to replace upload.php (upload.ashx)

Postby mike_something » Sat Apr 25, 2009 10:11 am

If you are using this and you need authentication/session details, this is a good discussion http://swfupload.org/forum/generaldiscussion/98
mike_something
 
Posts: 1
Joined: Sat Apr 25, 2009 10:09 am

Re: ASP.NET C# code to replace upload.php (upload.ashx)

Postby mark.rawlingson » Fri Jun 12, 2009 8:01 pm

I had trouble with the above code. I don't know why, I suspect it has something to do with requesting the physical application path - it may be returning the incorrect application root for me (i'll have to look, too lazy to verify that right now after struggling with this for 2+ hours)

Anyway, I rewrote it to this, and it works great now.

Code: Select all
using System;
using System.Web;
using System.IO;

public class Upload : IHttpHandler {
   public void ProcessRequest(HttpContext context) {         
     HttpPostedFile oFile = context.Request.Files["Filedata"];
     string sDirectory = HttpContext.Current.Server.MapPath(@context.Request["folder"]);
     oFile.SaveAs(sDirectory + oFile.FileName);
     if (!Directory.Exists(sDirectory)) Directory.CreateDirectory(sDirectory);
     context.Response.Write("1");
   }
   public bool IsReusable {
      get { return false; }
   }
}


Hope this helps someone - and thanks for the amazing script.
mark.rawlingson
 
Posts: 2
Joined: Fri Jun 12, 2009 7:50 pm

Re: ASP.NET C# code to replace upload.php (upload.ashx)

Postby iStan » Mon Aug 24, 2009 1:19 pm

And if you need to use sessions add the following next to IHttpHandler: IRequiresSessionState

And you can access the session variable like: context.Session("XXX");
User avatar
iStan
 
Posts: 116
Joined: Mon Aug 24, 2009 12:57 pm
Location: Alicante, Spain

Re: ASP.NET C# code to replace upload.php (upload.ashx)

Postby Jack » Tue Aug 25, 2009 2:54 pm

Does not seem to be working for me. Any tips on how to begin trying to debug this?
Jack
 
Posts: 8
Joined: Tue Aug 25, 2009 2:51 pm

Re: ASP.NET C# code to replace upload.php (upload.ashx)

Postby iStan » Tue Aug 25, 2009 3:59 pm

Here this works great in Internet Explorer 8, but It seems not to work in Firefox. I opened a thread on the ASP.NET forum to find out what is causing this problem.

In what browser are you testing the generic handler?
User avatar
iStan
 
Posts: 116
Joined: Mon Aug 24, 2009 12:57 pm
Location: Alicante, Spain

Re: ASP.NET C# code to replace upload.php (upload.ashx)

Postby Jack » Tue Aug 25, 2009 4:04 pm

IE8. I think there is something wrong with the upload folder as I am trying to use this with sharepoint. Is it possible to have the upload folder outside the web root i.e. c:/uploads/?
Last edited by Jack on Wed Aug 26, 2009 10:07 am, edited 1 time in total.
Jack
 
Posts: 8
Joined: Tue Aug 25, 2009 2:51 pm

Re: ASP.NET C# code to replace upload.php (upload.ashx)

Postby Jack » Tue Aug 25, 2009 4:18 pm

I am getting the 100% complete message but I can't find the file any where....
Jack
 
Posts: 8
Joined: Tue Aug 25, 2009 2:51 pm

Re: ASP.NET C# code to replace upload.php (upload.ashx)

Postby iStan » Tue Aug 25, 2009 4:18 pm

yes you can, as long as the user that is running the httpcontext has permissions to create and write. A quick test let me save the file in the root of the server.

Code: Select all
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
      Dim httpFile As HttpPostedFile
      
      If (Not context.Request.Files("Filedata") Is Nothing) Then
         httpFile = context.Request.Files("Filedata")
         
         Dim iFileLength As Integer = httpFile.ContentLength
      
         If (iFileLength <> 0) Then
            Dim targetDirectory As String = "c:\"
            Dim targetFilePath As String = Path.Combine(targetDirectory, httpFile.FileName)

            ' Uncomment the following line if you want to make the directory if it doesn't exist
            If (Not Directory.Exists(targetDirectory)) Then
               Directory.CreateDirectory(targetDirectory)
            End If

            httpFile.SaveAs(targetFilePath)
          
            context.Response.Write("1")
         End If
      End If
   End Sub
User avatar
iStan
 
Posts: 116
Joined: Mon Aug 24, 2009 12:57 pm
Location: Alicante, Spain

Re: ASP.NET C# code to replace upload.php (upload.ashx)

Postby iStan » Tue Aug 25, 2009 4:20 pm

Try to debug the generic handler like you normally do, set some breakpoints and step through the code, maybe an exception gets raised.
User avatar
iStan
 
Posts: 116
Joined: Mon Aug 24, 2009 12:57 pm
Location: Alicante, Spain

Re: ASP.NET C# code to replace upload.php (upload.ashx)

Postby Jack » Tue Aug 25, 2009 4:47 pm

Code: Select all
public class Upload : IHttpHandler
{
    public void ProcessRequest(HttpContext context) {
    HttpPostedFile oFile = context.Request.Files["Filedata"];

   if (oFile != null) {
   const string sDirectory = "c:\\";
   if (!Directory.Exists(sDirectory))
   Directory.CreateDirectory(sDirectory);

   oFile.SaveAs(Path.Combine(sDirectory, oFile.FileName));

   context.Response.Write("1");
        }
        else {
            context.Response.Write("0");
        }
    }
    public bool IsReusable {
        get {
            return false;
        }
    }
}


Above is the C# code I have. Debugging does not seem to be working.The breakpoints are not being hit when I remotely connect to the WP3 processes. Will do some googling to see if I need to debug a ashx file differently.
Last edited by Jack on Wed Aug 26, 2009 2:50 pm, edited 1 time in total.
Jack
 
Posts: 8
Joined: Tue Aug 25, 2009 2:51 pm

Re: ASP.NET C# code to replace upload.php (upload.ashx)

Postby Jack » Wed Aug 26, 2009 9:44 am

Code: Select all
$('#fileUpload').uploadify({
        'uploader': '/_layouts/js/uploadify/uploadify.swf',
        'script': '/_layouts/other/Upload.ashx',
        'cancelImg': '/_layouts/js/uploadify/cancel.png',
        'auto': true,
        'onError': function(event, queueID, fileObj, errorObj) {
            alert('error');
        },
        'onComplete': function(event, queueID, fileObj, response, data) {
            alert('complete');
        }
    });


Both the onComplete and onError functions are not being hit. Any ideas?
Jack
 
Posts: 8
Joined: Tue Aug 25, 2009 2:51 pm

Re: ASP.NET C# code to replace upload.php (upload.ashx)

Postby Jack » Wed Aug 26, 2009 10:33 am

Jack
 
Posts: 8
Joined: Tue Aug 25, 2009 2:51 pm

Re: ASP.NET C# code to replace upload.php (upload.ashx)

Postby Jack » Wed Aug 26, 2009 2:51 pm

I have debugging working. When I navigate directly to the page 0 is written to the page. The debugger fires and everything is good. When the script run it's not hitting Upload.ashx as no breakpoints are hit. I imagine my reference to Upload.ashx is incorrect. I tried using http://mysite/_layouts/other/Upload.ashx in the js and still no joy...
Jack
 
Posts: 8
Joined: Tue Aug 25, 2009 2:51 pm

Re: ASP.NET C# code to replace upload.php (upload.ashx)

Postby Jack » Wed Aug 26, 2009 3:34 pm

After some testing the problem seems to be that it is asking me to log into Share Point again (I am logged in). This is causing it to trip over. Any ideas how to make sure that my authenication is picked up?
Jack
 
Posts: 8
Joined: Tue Aug 25, 2009 2:51 pm

Re: ASP.NET C# code to replace upload.php (upload.ashx)

Postby iStan » Wed Aug 26, 2009 7:16 pm

From Sharepoint I don't have any knowledge, but I managed to solve the problem with the Session state not available on FF or other browsers not Microsoft. It is caused by a bug in the Adobe Flash software. It is an interesting article because they are talking about authentication, so give it a shot.
http://swfupload.org/forum/generaldiscussion/98

An other thing that I am thinking of. Is it possible that you need to set explecit permission on the files used by uploadify? Like the swf, css and the generic handler?
Maybe your problem isn't caused by uploadify at all. Try to create an generic handler that returns an image or a text to try if this works.
User avatar
iStan
 
Posts: 116
Joined: Mon Aug 24, 2009 12:57 pm
Location: Alicante, Spain

Re: ASP.NET C# code to replace upload.php (upload.ashx)

Postby dhulting » Wed Oct 07, 2009 12:35 am

In response to mark.rawlingson's post:

The script works, but it has a missing / between the Directory and the filename.

I changed this line:

Code: Select all
oFile.SaveAs(sDirectory + oFile.FileName);


to this:

Code: Select all
oFile.SaveAs(sDirectory + "/" + oFile.FileName);


and all works great!
dhulting
 
Posts: 1
Joined: Wed Oct 07, 2009 12:30 am

Re: ASP.NET C# code to replace upload.php (upload.ashx)

Postby carrzkiss » Thu Apr 08, 2010 7:57 pm

Hello All;

I found this
http://www.aspsnippets.com/Articles/Mul ... P.Net.aspx
download
http://archive.aspsnippets.com/file.axd ... Upload.zip

Works great, and works across IE8, Chrome, FireFox.

---------
The following items I am working on but am having some issues.

#1: Thumbnail - Make thumbnails of the images that are uploaded to the server.
I have a couple of Thumbnail scripts, but cannot get them to work with the Uploader.

#2: Save image Names (Not files but the FileName) to database. (Examples can be in Access, if they work in Access they will work across Access, SQL Server, MySQL and Oracle.

Any assistance on this would be great.

Thank You
Carrzkiss
carrzkiss
 
Posts: 2
Joined: Thu Apr 08, 2010 7:46 pm

Re: ASP.NET C# code to replace upload.php (upload.ashx)

Postby Rad101 » Thu Apr 29, 2010 9:25 am

Hi

I can get Uploadify to work perfectly on IE but not on Chrome, Safari or Firefox.

The button appears and I can select multiple files but when I hit the start upload link, it loads my login page that I've declared in Forms Authentication.

I've added breakpoints in the Upload.ashx but it just doesn't go in.

Any ideas will be appreciated.
Rad101
 
Posts: 22
Joined: Wed Jan 27, 2010 3:10 pm

Re: ASP.NET C# code to replace upload.php (upload.ashx)

Postby Rad101 » Thu Apr 29, 2010 4:14 pm

Anything would be good.
I've scoured the net looking but can't get anything to work.
Rad101
 
Posts: 22
Joined: Wed Jan 27, 2010 3:10 pm

Re: ASP.NET C# code to replace upload.php (upload.ashx)

Postby mark.rawlingson » Tue Jun 01, 2010 2:40 pm

dhulting wrote:In response to mark.rawlingson's post:

The script works, but it has a missing / between the Directory and the filename.

I changed this line:

Code: Select all
oFile.SaveAs(sDirectory + oFile.FileName);


to this:

Code: Select all
oFile.SaveAs(sDirectory + "/" + oFile.FileName);


and all works great!


Glad it worked for you. My bad re the missing "/" - I might have been passing that through the uploadify ajax request as part of my folder name. Doh.
mark.rawlingson
 
Posts: 2
Joined: Fri Jun 12, 2009 7:50 pm


Return to Showcase

Who is online

Users browsing this forum: No registered users and 0 guests