Okay, so I’ve got this code (in VB, which I don’t really know) that gets files from a flash file reference list and uploads them:
<%@ Import Namespace=“System.IO” %>
<script language=“vbscript” runat=“server”>
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim hfc As HttpFileCollection = Request.Files
For i As Integer = 0 To hfc.Count - 1
Dim hpf As HttpPostedFile = hfc(i)
If hpf.ContentLength > 0 Then
'Save the file to some mapped location i.e. “MyFiles”
hpf.SaveAs(Server.MapPath(“MyFiles”) & "" & Path.GetFileName(hpf.FileName))
End If
Next i
Catch ex As Exception
'some backend error handle
End Try
'Response.Write(“Hello”)
End Sub
This works perfectly… I don’t know how to use VB properly tho and there’s other features I wanna add, so I’m trying to convert this snippet to C#, where am I going wrong?:
protected void Page_Load(object sender, EventArgs e)
{
HttpFileCollection hfc = Request.Files;
int i;
for (i = 0; i<hfc.Count-1; i++) {
HttpPostedFile hpf = hfc*;
if (hpf.ContentLength > 0) {
hpf.SaveAs(Server.MapPath("MyFiles") + "\\" + Path.GetFileName(hpf.FileName));
}
}
}
I’ve added “using System.IO” at the top (I normally prefer to keep my code in a seperate to the aspx page)… But this doesn’t work… Why?
http://labs.developerfusion.co.uk/convert/vb-to-csharp.aspx
Its what the converter is made for works 80% of the time. Then the rest has to be done by hand which is usually minor things. (its made for windows development, so You will have to put in the using part and it should work.
public void Page_Load(object sender, [EventArgs](http://labs.developerfusion.co.uk/SourceViewer/search/SSCLI/EventArgs/) e)
{
try {
[HttpFileCollection](http://labs.developerfusion.co.uk/SourceViewer/search/SSCLI/HttpFileCollection/) hfc = Request.Files;
for (int i = 0; i <= hfc.Count - 1; i++) {
[HttpPostedFile](http://labs.developerfusion.co.uk/SourceViewer/search/SSCLI/HttpPostedFile/) hpf = hfc(i);
if (hpf.ContentLength > 0) {
//Save the file to some mapped location i.e. "MyFiles"
hpf.SaveAs(Server.MapPath("MyFiles") + "\\" + Path.GetFileName(hpf.FileName));
}
}
}catch (Exception ex) {
//some backend error handle
}
//Response.Write("Hello")
}
I can only convert C# to vb.net though by hand I don’t know enough C#, probably just wait till one of the many C# users comes by if the online converter doesn’t do it for you.
[whisper]//edit… also this is suppose to be in server-side this section is for non-web based programming[/whisper]
Hello!
Request with files is a first request to mentioned .aspx page or is it post back?
If it is post back, then you should add if(IsPostBack){} condition for all code inside Page_Load procedure.