【问题标题】:Convert PHP to ASP.Net for file upload将 PHP 转换为 ASP.Net 以进行文件上传
【发布时间】:2014-12-24 17:36:45
【问题描述】:

我正在尝试允许用户将图片上传到我的网站。我找到了一个演示,但它是用 PHP 编写的。我在 Webmatrix 中使用 CSHTML,它似乎与 PHP 文件不兼容。有没有人有任何资源、建议或信息链接来编写如下类似代码但格式与 Webmatrix/ASP.NET 兼容?另外,有没有办法在上传文件时重命名文件并将其保存到某个位置?

在允许用户上传到网站时,我应该采取任何安全预防措施吗?

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

}

【问题讨论】:

    标签: c# php asp.net image file-upload


    【解决方案1】:

    使用文件上传.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 
    
                        }
                    });
    }
    

    后面的代码

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

    【讨论】:

    • 感谢您的回复!我不确定在哪里放置上述代码。我正在使用此处找到的站点点的拖放模型:sitepoint.com/html5-file-drag-and-drop。基于该方法,我应该在哪里插入您建议的代码?谢谢。
    猜你喜欢
    • 2013-05-01
    • 2010-12-23
    • 1970-01-01
    • 2019-12-22
    • 2021-03-27
    • 2018-03-23
    • 2011-08-31
    • 2020-06-28
    • 1970-01-01
    相关资源
    最近更新 更多