2014-10-29 20 views
0

我试图让用户上传图像到我的网站。我发现了一个演示,但它是用PHP编写的。我在Webmatrix中使用CSHTML,它似乎与PHP文件不兼容。有没有人有任何资源,建议,或链接到信息编写类似的代码如下,但与Webmatrix/ASP.NET兼容的格式?另外,有没有办法在文件上传时重命名文件并将其保存到特定位置?将PHP转换为用于文件上传的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>"; 
     } 
    } 

} 

回答

0

使用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 

        } 
       }); 
} 

代码

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

谢谢你的回复!我不确定在哪里放置上面的代码。我正在从sitepoint的拖放模型中找到:http://www.sitepoint.com/html5-file-drag-and-drop/。基于该方法,我应该在哪里插入您的建议代码?谢谢。 – user44129 2015-02-14 05:36:19