views:

64

answers:

1

Hi All,

I posted something similar but I am changing my question around enough to warrant a new post.

I am trying to handle a posted file to an aspx page (C#). I am told this is not possible without a postback from <input type=file> or the asp file uploader. However, I have a PHP script does this perfectly, so I am really hoping it can be accomplished in C#/ASP.NET.

I am trying to post an image form a mobile device, so an actual <input type=file> is not an option.

Any thoughts and advise would really help!

PHP:

<?php

$uploaddir = 'uploads/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        echo "http://urlgoeshere.om/uploads/{$file}";
}
else {
    echo "No files uploaded\n";
}

?>

How can this be done in ASP.NET?

A: 

Based on code from here

This seems to do the job at accepting a random file upload.

 protected void Page_Load(object sender, EventArgs e)
    {
        string filePath = "uploads/";

        if (Request.Files.Count <= 0)
        {
            Response.Write("No file uploaded");
        }
        else
        {
            for (int i = 0; i < Request.Files.Count; ++i)
            {
                HttpPostedFile file = Request.Files[i];
                file.SaveAs(Server.MapPath(filePath + file.FileName));
                Response.Write("File uploaded");
            }
        }

    }
Martin Smith
Works like a charm! Thanks.
Brett