美文网首页
2018-05-29上传图片

2018-05-29上传图片

作者: NOTEBOOK2 | 来源:发表于2018-05-29 14:47 被阅读0次

上传图片,以订购单为例:
step01: uploadImage
src/app/modules/purchase_orders/purchaseOrderFactory.js

  var createPurchaseOrder = function(order, items, store_id) {
    order.store_id = store_id;
    return $http.post($rootScope.gateway + '/v2/stores/' + store_id + '/purchase_orders',
      purchaseOrderMapper(order, items));
  };   

  var uploadPurchaseOrderImage = function(id, file) {
    var fd = new FormData();
    fd.append('image', file);
    return $http.post($rootScope.gateway + '/v2/stores/' + DashboardFactory.getStoreId() + '/purchase_orders/' + id + '/purchase_order_images', fd, {
      transformRequest: angular.identity,
      headers: { 'Content-Type': undefined }
    });
  }; 

step02: 处理逻辑
src/app/modules/purchase_orders/purchaseOrdersController.js

PurchaseOrderFactory.createPurchaseOrder($scope.order, items, $scope.current_store_id)
  .success(function (data) {
    if ($scope.orderImages.length) {
      _.each($scope.orderImages, function (file, i) {
        PurchaseOrderFactory.uploadPurchaseOrderImage(data.purchase_order.id, file)
          .success(function (data) {
            if (i === $scope.orderImages.length - 1) {
              $state.go('app.dashboard.purchase-orders.index', { store_id: DashboardFactory.getStoreId() });
            }
          })
          .error(function (err) {
            alert('Image uploading failed.');
            $state.go('app.dashboard.purchase-orders.index', { store_id: $stateParams.store_id });
          });
      });
    } else {
      $state.go('app.dashboard.purchase-orders.index', { store_id: $stateParams.store_id });
    }
  });

折扣部分的改动:
src/app/modules/discounts/discountFactory.js

  var uploadAdvancedDiscountImage = function(id, file) {
    var fd = new FormData();
    fd.append('image', file);
    return $http.post($rootScope.gateway + '/api/v4/stores/' + DashboardFactory.getStoreId() + '/discounts/' + id + '/discount_images', fd, {
      transformRequest: angular.identity,
      headers: { 'Content-Type': undefined }
    });
  };  

src/app/modules/discounts/advancedDiscountsController.js

// 497  
    $scope.orderImages = [];
    $scope.addImage = function (file, allFiles) {
      $scope.orderImages = allFiles;
    };

538-540

            if ($scope.orderImages.length) {
              _.each($scope.orderImages, function (file, i) {
                DiscountFactory.uploadAdvancedDiscountImage(data.discount.id, file)
                  .success(function (data) {
                    if (i === $scope.orderImages.length - 1) {
                      $scope.isSubmitting = false;
                      $state.go('app.dashboard.discounts.index', { store_id: DashboardFactory.getStoreId() });
                    }
                  })
                  .error(function (err) {
                    alert('Image uploading failed.');
                    $state.go('app.dashboard.discounts.index', { store_id: DashboardFactory.getStoreId() });
                  })
              })
            } else {
              $scope.isSubmitting = false;
              $state.go('app.dashboard.discounts.index', { store_id: DashboardFactory.getStoreId() });
            }

src/app/modules/discounts/advanced_discount_form.html

        <div class='panel-section'>
            <h3>{{'Images' |translate}}</h3>
            <div class="xgrid -gallery">
                <div ng-repeat='image in discount_images' class="xgrid-cell -default-25">
                    <img ng-src="{{image.image_url}}" alt="{{image.note}}" class="_rs-image">
                </div>
            </div>
            <input-image title='Add Image' on-select='addImage' ng-show="editMode"></input-image>
        </div>  

相关文章

网友评论

      本文标题:2018-05-29上传图片

      本文链接:https://www.haomeiwen.com/subject/lxbojftx.html