将这些PhP脚本转换为Web页面Razor语法

时间:2014-12-21 15:01:02

标签: php asp.net froala

我刚接触到这个Froala Editpr,遗憾的是,服务器集成脚本是用php编写的,我在Asp.net网页Razor语法中使用它。

// Allowed extentions.
$allowedExts = array("gif", "jpeg", "jpg", "png");

// Get filename.
$temp = explode(".", $_FILES["file"]["name"]);

// Get extension.
$extension = end($temp);

// An image check is being done in the editor but it is best to
// check that again on the server side.
// Do not use $_FILES["file"]["type"] as it can be easily forged.
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $_FILES["file"]["tmp_name"]);

if ((($mime == "image/gif")
|| ($mime == "image/jpeg")
|| ($mime == "image/pjpeg")
|| ($mime == "image/x-png")
|| ($mime == "image/png"))
&& in_array($extension, $allowedExts)) {
    // Generate new random name.
    $name = sha1(microtime()) . "." . $extension;

    // Save file in the uploads folder.
    move_uploaded_file($_FILES["file"]["tmp_name"], getcwd() . "/uploads/" . $name);

    // Generate response.
    $response = new StdClass;
    $response->link = "/uploads/" . $name;
    echo stripslashes(json_encode($response));
}

1 个答案:

答案 0 :(得分:1)

试试这些代码块

if(IsPost) {
              var file = Request.Files[0];

         var fileName = Path.GetFileName(file.FileName);
                    var rootPath = Server.MapPath("/img/uploads/");
                    file.SaveAs(Path.Combine(rootPath, fileName));

   //     Json.Write(fileSavePath, Response.Output);
         var json = Html.Raw(Json.Encode(new { link = @"/img/uploads/" + fileName}));
                                      Response.ContentType = "text/plain";
                    Response.Write(json);
                    Response.End();
    }