如何以及在哪里使用ng-flow保存上传的文件?

时间:2014-05-22 15:51:44

标签: javascript php angularjs flow-js

首先,我使用ng-flow(angular.js框架上的html5文件上传扩展名)

我的文件已上传,我在控制台中记录了该事件。 但我不明白在哪里以及如何保存它们。

这是我的html代码,上传被调用。

<div flow-init flow-files-submitted="$flow.upload()">   
<div class="drop" flow-drop ng-class="dropClass">
    <span class="btn btn-default" flow-btn>Upload File</span>
    <span class="btn btn-default" flow-btn flow-directory ng-show="$flow.supportDirectory">Upload Folder</span>
    <b>OR</b>
    Drag And Drop your file here
</div>

这是我的配置

app.config(['flowFactoryProvider', function (flowFactoryProvider) {
  flowFactoryProvider.defaults = {
    target: 'upload.php',
    permanentErrors: [404, 500, 501],
    maxChunkRetries: 1,
    chunkRetryInterval: 5000,
    simultaneousUploads: 4,
    singleFile: true
  };
  flowFactoryProvider.on('catchAll', function (event) {
    console.log('catchAll', arguments);
  });
  // Can be used with different implementations of Flow.js
  // flowFactoryProvider.factory = fustyFlowFactory;
}]);

upload.php被调用,$_GET已满数据,

<script>alert('alert' + array(8) {
  ["flowChunkNumber"]=>
  string(1) "1"
  ["flowChunkSize"]=>
  string(7) "1048576"
  ["flowCurrentChunkSize"]=>
  string(6) "807855"
  ["flowTotalSize"]=>
  string(6) "807855"
  ["flowIdentifier"]=>
  string(11) "807855-3png"
  ["flowFilename"]=>
  string(5) "3.png"
  ["flowRelativePath"]=>
  string(5) "3.png"
  ["flowTotalChunks"]=>
  string(1) "1"
}
)</script>

但是当我在这里时,我需要做些什么来保存我的文件?

我尝试move_uploaded_file()flowFilename flowRelativePath,但没有任何附加内容。

我是js的新人。

谢谢。

4 个答案:

答案 0 :(得分:4)

查看upload.php示例脚本:

https://github.com/flowjs/flow.js/blob/master/samples/Backend%20on%20PHP.md

// init the destination file (format <filename.ext>.part<#chunk>
// the file is stored in a temporary directory
$temp_dir = 'temp/'.$_POST['flowIdentifier'];
$dest_file = $temp_dir.'/'.$_POST['flowFilename'].'.part'.$_POST['flowChunkNumber'];

答案 1 :(得分:2)

使用flow.js上传图像后,会向您的服务器发送新的帖子请求。您需要处理此POST请求并在之后操作该文件。

如果您使用的是Java + Spring MVC,它看起来像

@RequestMapping(value = "/upload",
        method = RequestMethod.POST,
        produces = MediaType.APPLICATION_JSON_VALUE)
public void handleFileUpload(@RequestParam("file") MultipartFile file) {
    log.debug("REST request to handleFileUpload");
    try {
        BufferedOutputStream stream =
                new BufferedOutputStream(new FileOutputStream(new File(path + file.getName())));
        stream.write(file.getBytes());
        stream.close();
        log.debug("You successfully uploaded " + file.getName() + "!");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

答案 2 :(得分:0)

我只花了半天时间使用ng-flow,并希望将解决方案发布到PHP上。它没有利用分块和恢复功能,我只需要在没有页面刷新的情况下上传的内容。

首先, flow-init =&#34; {target:&#39; / upload&#39;,testChunks:false}&#34; 实施例

<div flow-init="{target: '/upload', testChunks:false}" flow-files-submitted="$flow.upload()" flow-file-success="$file.msg = $message">
            <input type="file" flow-btn />
            <span flow-btn>Upload File</span>
</div>

第二,

现在应该将请求发送到&#34; / upload&#34; .....在该请求中存在$ _FILES数组。

一行代码为我保存了文件:

$result=move_uploaded_file($_FILES['file']['tmp_name'],'yourfilename');

如果您希望通过角度控制器控制此值,可以设置如下值:

    $scope.uploader={};
    $scope.upload = function (id) {

        $scope.uploader.flow.opts.testChunks=false;
        $scope.uploader.flow.opts.target='/upload;
        $scope.uploader.flow.upload();
    }

并在你的html中添加:

<div flow-init flow-name="uploader.flow">
<button flow-btn>Add files</button>
<div>

答案 3 :(得分:0)

创建名为上传的文件夹意味着您将临时文件移至此处,然后在php脚本中添加代码。

$uploads_dir = 'uploads';
$target_file = $uploads_dir .'/'. basename($_FILES['file']['name']);
move_uploaded_file($_FILES['file']['tmp_name'],$target_file);