开发者

ASP.NET File Upload - > Translation from PHP

开发者 https://www.devze.com 2023-01-05 19:12 出处:网络
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 postbac

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:开发者_如何学Go

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

    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号