简体   繁体   English

将PHP转换为ASP.Net以进行文件上传

[英]Convert PHP to ASP.Net for file upload

I'm trying to allow users to upload an image to my site. 我正在尝试允许用户将图像上传到我的网站。 I found a demo but it is written in PHP. 我找到了一个演示,但是它是用PHP编写的。 I am using CSHTML in Webmatrix, and it doesn't seem to be compatible with the PHP file. 我在Webmatrix中使用CSHTML,它似乎与PHP文件不兼容。 Does anyone have any resources, recommendations, or links to information for writing a similar code as below but in a compatible format to Webmatrix/ASP.NET? 任何人都没有任何资源,建议或信息链接,可用于编写以下类似代码,但格式与Webmatrix / ASP.NET兼容。 Also, is there a way to rename the file when it's uploaded and save it to a certain location? 另外,有没有一种方法可以在文件上传后重命名并将其保存到特定位置?

Are there any security precautions I should take when allowing users to upload to the site? 允许用户上传到网站时,我应该采取什么安全预防措施?

$fn = (isset($_SERVER['HTTP_X_FILENAME']) ? $_SERVER['HTTP_X_FILENAME'] : false);

if ($fn) {

   // AJAX call
   file_put_contents(
      'uploads/' . $fn,
      file_get_contents('php://input')
   );
   echo "$fn uploaded";
   exit();

}
else {

   // form submit
   $files = $_FILES['fileselect'];

   foreach ($files['error'] as $id => $err) {
      if ($err == UPLOAD_ERR_OK) {
         $fn = $files['name'][$id];
         move_uploaded_file(
             $files['tmp_name'][$id],
             'uploads/' . $fn
         );
         echo "<p>File $fn uploaded.</p>";
      }
   }

}

use fileupload.js 使用fileupload.js

<script src="@Url.Content("/Scripts/jquery.fileupload.js")" type="text/javascript"></script>

<input type="file" name="resume" onchange="onChangeResume()"  id="txtImageUpload"  style="width: 76px;"  />

function onChangeResume(){
    $('#txtImageUpload').fileupload({
                    dataType: 'json',
                    url: '/CandidateManagement.aspx/ImageUpload',
                    autoUpload: true,
                    done: function (e, data) {

                        //Statements 

                    }
                });
}

code behind 背后的代码

  HttpPostedFileBase resume = Request.Files["txtImageUpload"];
        if (resume != null && resume.ContentLength > 0)
        {
            var fileName = Path.GetFileName(resume.FileName);
            var path = Path.Combine(Server.MapPath("~/Resumes"), fileName);//you can give what ever you want
            resume.SaveAs(path);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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