0

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.

MMzztx
  • 243
  • 5
  • 15
  • Please be more specific about your problem. I have no idea what you mean.. – aLx13 Jan 13 '17 at 16:17
  • Not seeing any PHP here... – wogsland Jan 13 '17 at 16:18
  • @wogsland there is nothing about php actually, my bad, it is just simple problem about how to use existed file on server as file object – MMzztx Jan 13 '17 at 16:23
  • What file object like a php file pointer or a javascript file storage object? – aLx13 Jan 13 '17 at 16:26
  • Use ajax to do this: `$.get('path/to/file', function(data) { console.log(data); });` – Al.G. Jan 13 '17 at 16:36
  • @b3wii let me make it more specific – MMzztx Jan 13 '17 at 16:36
  • @b3wii I have tried $.get, and using this method console shows me some binary like(DICMUL�OBUI1.2.840.10008.5.1.4.1.1.4UI@1.3.6.1.4.1.14519.5.2.1.7311.5101.229050669731849304180662766890UI1.2.840.10008.1.2.1UI1.2.40.0.13.1.1.1SHdcm4che-1.4.35CS), but when I use ’var file = e.target.file;‘ console.log(file) shows me a object with file info(like lastModified:.....name:...size:...), so do I need do some convert of that from your method to that object? – MMzztx Jan 13 '17 at 17:15
  • @MMzztx Maybe this helps: https://www.html5rocks.com/en/tutorials/file/dndfiles/ – aLx13 Jan 16 '17 at 08:47

1 Answers1

0

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

Bambang
  • 391
  • 1
  • 10