Невозможно загрузить файл на сервер (AngularJS и Perl)

0

Это мой код загрузки файлов.

<input type="file" name="file" onchange="angular.element(this).scope().uploadFile(this.files)"/>


$scope.uploadFile = function(files) {
    var fd = new FormData();
    //Take the first selected file
    fd.append("file", files[0]);

    $http.post("http://admin.localhost/cgi-bin/upload.pl", fd, {
        withCredentials: true,
        headers: {'Content-Type': undefined },
        transformRequest: angular.identity
    }).success(function(){
            alert ('success');
        })
        .error(function(){
            alert ('error');
        })
};

Код Perl

#!c:/perl64/bin/perl.exe

use strict;
use warnings;
use DBI;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use CGI;
use CGI qw(:standard);
use JSON;
use CGI::Carp qw ( fatalsToBrowser );
use File::Basename;

#send the obligatory Content-Type
print "Content-Type: text/html\n\n"; 

$CGI::POST_MAX = 1024 * 5000;
my $safe_filename_characters = "a-zA-Z0-9_.-";

my $cgi = new CGI();
my $upload_dir = "admin.localhost/images/dummy"; #location on our server  
my $filename = "dummy.png";

my $upload_filehandle = $cgi->upload("file");
print $upload_filehandle;

open UPLOADFILE, ">../..$upload_dir$filename"  or die $!;
binmode UPLOADFILE;

while ( <$upload_filehandle> )
{
    print UPLOADFILE;
}
close UPLOADFILE;

Хотя предупреждение в контроллере идет успешно, но файл не загружается на сервер.

Может ли кто-нибудь помочь мне, что им не хватает или что не так?

Теги:

1 ответ

0

Попробуйте использовать файловую модель, этот код работает для меня

<input type="file" file-model="myFile"/>

var fd = new FormData();
fd.append('file', $scope.myFile);
$http.post(uploadUrl, fd, {
  transformRequest: angular.identity,
  headers: {'Content-Type': undefined}
})
.success(function(){
 //success
})
 .error(function(){
 //failure
});

Ещё вопросы

Сообщество Overcoder
Наверх
Меню