[英]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#). 我正在尝试将发布的文件处理到aspx页面(C#)。 I am told this is not possible without a postback from
<input type=file>
or the asp file uploader. 有人告诉我,没有
<input type=file>
或asp文件上传器的回发是不可能的。 However, I have a PHP script does this perfectly, so I am really hoping it can be accomplished in C#/ASP.NET. 但是,我有一个PHP脚本可以完美地做到这一点,因此我真的希望可以在C#/ ASP.NET中完成它。
I am trying to post an image form a mobile device, so an actual <input type=file>
is not an option. 我正在尝试从移动设备发布图像,因此实际的
<input type=file>
不是一个选择。
Any thoughts and advise would really help! 任何想法和建议都将真正有帮助!
PHP: 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? 如何在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");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.