草庐IT

php - 图像未从 Angular 上传到 Laravel

coder 2024-04-17 原文

我创建了一个用于保存带有图像的项目详细信息的表单。我正在使用 laravel 5.1 和 angularJS。项目信息将成功保存,但图像未上传到服务器,图像详细信息也未保存在数据库中。我需要帮助使用 angularjs 将图像上传到 laravel

这是我的 HTML 代码:

    <div ng-controller="itemController">
<form ng-submit="addItem()">
    <label>Name:</label><input type="text" name="name" value="" ng-model="newitem.name" placeholder="Item Name">
    <label>Model No:</label><input type="text" name="model" value="" ng-model="newitem.model" placeholder="Model Number">
    <label>Size:</label><input type="text" name="size" value="" ng-model="newitem.size" placeholder="Item Size">
    <label>Colour:</label><input type="text" name="color" value="" ng-model="newitem.color" placeholder="Item Colour">
    <br>
    <label>Description:</label><textarea cols="30" rows="5" ng-model="newitem.description" placeholder="Description"></textarea>
    <br>
    <label>Photo:</label><input type="file"  accept="imag/works" ngf-select="" ngf-multiple="true"  class="form-control" id="img" name="img" placeholder="Image" ng-model="newitem.photo" multiple>
    <br>
    <button type="submit">Save</button>
</form>
<div ng-show="sendmessage">
    Item Saved Successfully...........
</div>

<div>

AngularJs 代码:

app.controller('itemController',function($scope,$http,Item) {
$scope.items=[];
$scope.newitem={};
$scope.curitem = {};
$scope.sendmessage=false;

loadData();

//To Send Message to Site Admin..................

$scope.addItem=function(){
    if($scope.curitem.id){
        //TODO error display
        $scope.newitem.$update(function () {
            angular.extend($scope.curitem,$scope.curitem, $scope.newitem);
            $scope.sendmessage=true;
        });
    }else{
        $scope.newitem.$save(function (item) {
            //TODO error display
            $scope.items.push(item);
            $scope.sendmessage=true;
        });
    }
    //$scope.sendmessage=true;
    $scope.curitem = {};
    $scope.newitem = {};
    $scope.newitem=new Item();
    loadData();
};



function loadData(){
    var items =Item.query(function () {
        $scope.items = items;
        $scope.newitem=new Item();
    });
}
});


angular.module('ItemService',[]).factory('Item',['$resource',
function($resource){
    return $resource('/api/items/:itemId',{
        itemId:'@id'
    },{
        update:{
            method:'PUT'
        }
    });
}
]);

ItemController.php 如下...

        <?php

        namespace App\Http\Controllers\Auth;

        use App\File;
        use App\Item;
        use Illuminate\Http\Request as Request;
        use App\Http\Requests;
        use App\Http\Controllers\Controller;
        use Illuminate\Http\Response;
        use Validator;
        use Input;

       class ItemController extends Controller
       {
              /**
             * Validates given data for account
 * @param array $data
 * @return Validator
 */

protected function validator(array $data)
{
    // Loan account is not validated

    return Validator::make($data,[
        'name'  => 'required',
        'size'   => 'required|max:255'
    ]);

}


/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $validator = $this->validator($request->all());
    if($validator->fails()){
        return Response::json( $validator->errors()
            ,400);
    }
    $item=new Item($request->all());
    if($item->save())
    {
        $id=$item->id;
        if ($request->hasFile('photo')) {
            $file = $request->file('photo');
            $image=new File();
            $image->name = $this->uploadImage($file);
            $image->file_id=$id;
            $image->save();
        }
    }

    return Response::json(['error' => 'Server is down']
        ,500);
}


/**
 * To Strore Images
 *
 *
 */

public  function uploadImage($file){

        $storedFileName="";
        if(!empty($file)){

            $extension=$file->getClientOriginalExtension();
            $fileName = rand(11111,99999).'.'.$extension;
            $storedFileName=base_path().'/img/works/'. $fileName;
            $file->move(
                base_path().'/img/works/', $fileName
            );
        }
    return $storedFileName;
}


}

最佳答案

要通过 AngularJS 上传图片,您需要做一些额外的工作。您可以使用像 nervgh/angular-file-upload 这样的库(或其他图书馆)或自己做。更多引用 watch this tutorial by egghead

关于php - 图像未从 Angular 上传到 Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33030970/

有关php - 图像未从 Angular 上传到 Laravel的更多相关文章

  1. ruby-on-rails - 添加回形针新样式不影响旧上传的图像 - 2

    我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司

  2. ruby-on-rails - 在 Ruby (on Rails) 中使用 imgur API 获取图像 - 2

    我正在尝试使用Ruby2.0.0和Rails4.0.0提供的API从imgur中提取图像。我已尝试按照Ruby2.0.0文档中列出的各种方式构建http请求,但均无济于事。代码如下:require'net/http'require'net/https'defimgurheaders={"Authorization"=>"Client-ID"+my_client_id}path="/3/gallery/image/#{img_id}.json"uri=URI("https://api.imgur.com"+path)request,data=Net::HTTP::Get.new(path

  3. python ffmpeg 使用 pyav 转换 一组图像 到 视频 - 2

    2022/8/4更新支持加入水印水印必须包含透明图像,并且水印图像大小要等于原图像的大小pythonconvert_image_to_video.py-f30-mwatermark.pngim_dirout.mkv2022/6/21更新让命令行参数更加易用新的命令行使用方法pythonconvert_image_to_video.py-f30im_dirout.mkvFFMPEG命令行转换一组JPG图像到视频时,是将这组图像视为MJPG流。我需要转换一组PNG图像到视频,FFMPEG就不认了。pyav内置了ffmpeg库,不需要系统带有ffmpeg工具因此我使用ffmpeg的python包装p

  4. ruby - 是否有将图像文件转换为 ASCII 艺术的命令行程序或库? - 2

    有这样的事吗?我想在Ruby程序中使用它。 最佳答案 试试这个http://csl.sublevel3.org/jp2a/此外,Imagemagick可能还有一些东西 关于ruby-是否有将图像文件转换为ASCII艺术的命令行程序或库?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/6510445/

  5. ruby-on-rails - 使用 Dragonfly 从 URL 分配图像 - 2

    我正在使用Dragonfly在Rails3.1应用程序上处理图像。我正在努力通过url将图像分配给模型。我有一个很好的表格:{:multipart=>true}do|f|%>RemovePicture?Dragonfly的文档指出:Dragonfly提供了一个直接从url分配的访问器:@album.cover_image_url='http://some.url/file.jpg'但是当我在控制台中尝试时:=>#ruby-1.9.2-p290>picture.image_url="http://i.imgur.com/QQiMz.jpg"=>"http://i.imgur.com/QQ

  6. Ruby-vips 图像处理库。有什么好的使用示例吗? - 2

    我对图像处理完全陌生。我对JPEG内部是什么以及它是如何工作一无所知。我想知道,是否可以在某处找到执行以下简单操作的ruby​​代码:打开jpeg文件。遍历每个像素并将其颜色设置为fx绿色。将结果写入另一个文件。我对如何使用ruby​​-vips库实现这一点特别感兴趣https://github.com/ender672/ruby-vips我的目标-学习如何使用ruby​​-vips执行基本的图像处理操作(Gamma校正、亮度、色调……)任何指向比“helloworld”更复杂的工作示例的链接——比如ruby​​-vips的github页面上的链接,我们将不胜感激!如果有ruby​​-

  7. ruby-on-rails - 如何将大于 5GB 的文件上传到 Amazon S3? - 2

    我目前正在使用带有Carrierwavegem的Rails3.2将文件上传到AmazonS3。现在我需要能够处理用户提交的大于5GB的文件,同时仍然使用Carrierwavegem。Carrierwave或Fog是否有任何其他gem或分支可以处理5GB以上的文件上传到S3?编辑:我不想重写一个完整的Rails上传解决方案,所以像这样的链接没有帮助:https://gist.github.com/908875. 最佳答案 我想出了如何做到这一点,并且现在可以正常工作了。在正确的config/environment文件中,添加以下内容以

  8. ruby-on-rails - 如何播种图像的路径? - 2

    Organization和Image具有一对一的关系。Image有一个名为filename的列,它存储文件的路径。我在Assets管道中包含这样一个文件:app/assets/other/image.jpg。播种时如何包含此文件的路径?我已经在我的种子文件中尝试过:@organization=...@organization.image.create!(filename:File.open('app/assets/other/image.jpg'))#Ialsotried:#@organization.image.create!(filename:'app/assets/other/i

  9. ruby-on-rails - 安全地显示使用回形针 gem 上传的图像 - 2

    默认情况下:回形针gem将所有附件存储在公共(public)目录中。出于安全原因,我不想将附件存储在公共(public)目录中,所以我将它们保存在应用程序根目录的uploads目录中:classPost我没有指定url选项,因为我不希望每个图像附件都有一个url。如果指定了url:那么拥有该url的任何人都可以访问该图像。这是不安全的。在user#show页面中:我想实际显示图像。如果我使用所有回形针默认设置,那么我可以这样做,因为图像将在公共(public)目录中并且图像将具有一个url:Someimage:看来,如果我将图像附件保存在公共(public)目录之外并且不指定url(同

  10. ruby - Paperclip:以编程方式分配图像并设置其名称 - 2

    使用Paperclip,我想从这样的URL抓取图像:require'open-uri'user.photo=open(url)问题是我最后得到一个像“open-uri20110915-4852-1o7k5uw”这样的文件名。有什么方法可以更改user.photo上的文件名?作为一个额外的变化,Paperclip将我的文件存储在S3上,所以如果我可以在初始分配中设置我想要的文件名就更好了,这样图像就会上传到正确的S3key。像这样:user.photo=open(url),:filename=>URI.parse(url).path 最佳答案

随机推荐