简体   繁体   中英

ASP.NET File Upload - > Translation from PHP

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?

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");
            }
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM