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

Posted:
Sun Mar 15, 2009 8:23 am
by TravisN.
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; }
}
}
Re: ASP.NET C# code to replace upload.php (upload.ashx)

Posted:
Sat Apr 25, 2009 10:11 am
by mike_something
If you are using this and you need authentication/session details, this is a good discussion
http://swfupload.org/forum/generaldiscussion/98
Re: ASP.NET C# code to replace upload.php (upload.ashx)

Posted:
Fri Jun 12, 2009 8:01 pm
by mark.rawlingson
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.
Re: ASP.NET C# code to replace upload.php (upload.ashx)

Posted:
Mon Aug 24, 2009 1:19 pm
by iStan
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");
Re: ASP.NET C# code to replace upload.php (upload.ashx)

Posted:
Tue Aug 25, 2009 2:54 pm
by Jack
Does not seem to be working for me. Any tips on how to begin trying to debug this?
Re: ASP.NET C# code to replace upload.php (upload.ashx)

Posted:
Tue Aug 25, 2009 3:59 pm
by iStan
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?
Re: ASP.NET C# code to replace upload.php (upload.ashx)

Posted:
Tue Aug 25, 2009 4:04 pm
by Jack
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/?
Re: ASP.NET C# code to replace upload.php (upload.ashx)

Posted:
Tue Aug 25, 2009 4:18 pm
by Jack
I am getting the 100% complete message but I can't find the file any where....
Re: ASP.NET C# code to replace upload.php (upload.ashx)

Posted:
Tue Aug 25, 2009 4:18 pm
by iStan
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
Re: ASP.NET C# code to replace upload.php (upload.ashx)

Posted:
Tue Aug 25, 2009 4:20 pm
by iStan
Try to debug the generic handler like you normally do, set some breakpoints and step through the code, maybe an exception gets raised.
Re: ASP.NET C# code to replace upload.php (upload.ashx)

Posted:
Tue Aug 25, 2009 4:47 pm
by Jack
- 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.
Re: ASP.NET C# code to replace upload.php (upload.ashx)

Posted:
Wed Aug 26, 2009 9:44 am
by Jack
- 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?
Re: ASP.NET C# code to replace upload.php (upload.ashx)

Posted:
Wed Aug 26, 2009 10:33 am
by Jack
Re: ASP.NET C# code to replace upload.php (upload.ashx)

Posted:
Wed Aug 26, 2009 2:51 pm
by Jack
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...
Re: ASP.NET C# code to replace upload.php (upload.ashx)

Posted:
Wed Aug 26, 2009 3:34 pm
by Jack
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?
Re: ASP.NET C# code to replace upload.php (upload.ashx)

Posted:
Wed Aug 26, 2009 7:16 pm
by iStan
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/98An 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.
Re: ASP.NET C# code to replace upload.php (upload.ashx)

Posted:
Wed Oct 07, 2009 12:35 am
by dhulting
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!
Re: ASP.NET C# code to replace upload.php (upload.ashx)

Posted:
Thu Apr 08, 2010 7:57 pm
by carrzkiss
Hello All;
I found this
http://www.aspsnippets.com/Articles/Mul ... P.Net.aspxdownload
http://archive.aspsnippets.com/file.axd ... Upload.zipWorks 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
Re: ASP.NET C# code to replace upload.php (upload.ashx)

Posted:
Thu Apr 29, 2010 9:25 am
by Rad101
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.
Re: ASP.NET C# code to replace upload.php (upload.ashx)

Posted:
Thu Apr 29, 2010 4:14 pm
by Rad101
Anything would be good.
I've scoured the net looking but can't get anything to work.
Re: ASP.NET C# code to replace upload.php (upload.ashx)

Posted:
Tue Jun 01, 2010 2:40 pm
by mark.rawlingson
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.