dsxjot8620 2017-01-13 16:14
浏览 42
已采纳

如何请求单个文件作为对象

I just start to learn javascirpt, php about 2 days. The problem I face is I already have a x.dcm file under server root, and I already known that path(e.g. http://localhost:8888/....) My question is how can I simply grab that file from server to use, maybe something like:

var file= 'http://localhost:8888/....';      ////file is not an object

I ask this question because I already known how to use input method:

<input type="file" name="file" id="file">
<script>
    $('#file').on('change',function(e){
         var file = e.target.file;            ///file is an object
      });
</script>

but that is not what I want, what I want is to use an existed file rather than input.

So the whole thing is that:

<form id="input" method="post" enctype="multipart/form-data">
<input type="file" id="fileToUpload" name="fileToUpload">
</form>

I firstly make a input to upload some file,then in script

<script>

$("form#input").submit(function(){

    var formData = new FormData($(this)[0]);
$.ajax({
    url: 'segmentation.php',
    type: 'POST',
    data: formData,
    async: false,
    success: function (html) {
        $('#segbound').html(html);
    },
    cache: false,
    contentType: false,
    processData: false
});

return false;
});
</script>

I sent this file(e.g image.dcm) to do something( run a exec) on the server side, then it generates another image(imgproc.dcm) in an expected path(http://localhost:8888/....), and then the next thing is that I what that processed image display on the screen. To do that I need to use a js called cornerstone, and the function in it imageLoader.fileManager.get(file) which file is that one I what to display.

When I select from input using var file = e.target.file; as I mentioned above, it works perfect, then I check the file type it is a [file object]. But when I want to simply display that 'imgproc.dcm' by using var file= 'http://localhost:8888/....'; the file type is not an object which comes out my question, how can I simply grab that known path image to use as an object.

Or, to improve that, it is possible to get the return (generated imgproc.dcm) directly after it process on server side, and then to use that return(maybe give it an id...do not know) to display (call cornerstone function imageLoader.fileManager.get(file))

On server side, it looks like:

<?php
$target_dir = "/Applications/MAMP/htdocs/dicomread/temp/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if (file_exists($target_file)) {
    echo "file has already been uploaded.";
    $uploadOk = 0;
}


if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";

} else {
    if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_file)) {
        echo "The file ". basename( $_FILES['fileToUpload']['name']). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

    $cmd = "/Applications/MAMP/htdocs/dicomread/abc 2>&1";
    $Output_fileName = "imgproc.dcm";//$_FILES['fileToUpload']['name'];

    exec("$cmd $target_file $Output_fileName);

    echo "<br/>done";

?>

Any help would be appreciated.

  • 写回答

1条回答 默认 最新

  • dongyuanguang3893 2017-01-13 16:19
    关注

    Use fopen with URL to the file:

    $file = fopen("http://localhost:8888/x.dcm", "r");
    

    Refer to this for fopen: http://php.net/manual/en/function.fopen.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?