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

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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效