目的是点击按钮上传文件betway体育app:,然后初

ios-svn上传代码 使用工具cornerstone import上传文件 .a缺失的解决方法。

1.

betway体育app 1打开cornerstone-》preferences

2.

betway体育app 2save

3.可以解决啦~

 1     首先得创建个文件夹保存上传的文件;

刚做了头像上传,下面把整个流程走一遍,希望共同学习,共同进步!

最近做的项目中用到了angular下上传图片功能,在做的过程中遇到了许多问题,最终都得以解决

文件上传

 2     在项目里加一个上传文件的控件FileUpload和按钮,目的是点击按钮上传文件,效果如图所示:

  • 1.首先调用手机系统相册或者摄像头,然后获取到图片,最后把获取的图片上传到服务器!
  • 2.应该在上传至服务器成功之后保存头像,并更新显示用户头像,这是整个大致结构!

angular上传时和普通上传时过程差不多,只不过是要不一些东西转化为angular的东西。

准备工作 1.文件上传的页面 2.上传文件要保存的文件夹

                                                                       betway体育app 3

下面我们看demo(本文用UIImagePickerController类上传头像):
首先创建一个全局的UIImagePickerController对象 pickerController 然后初始化对象:

1.ng-file-select,指令angular是没此功能的,其实也是转化成了change事件,不多说,直接上代码

1.只要将文件传上来就行  

 3     上传文件 :用SaveAs方法,代码如下:

这时候要牵几个代理

angular.module('myApp')
.directive('ngFileSelect', [ '$parse', '$timeout', function($parse, $timeout) {
    return function(scope, elem, attr) {
      var fn = $parse(attr['ngFileSelect']);
      elem.bind('change', function(evt) {
        var files = [], fileList, i;
        fileList = evt.target.files;
        if (fileList != null) {
          for (i = 0; i < fileList.length; i++) {
            files.push(fileList.item(i));
          }
        }
        $timeout(function() {
          fn(scope, {
            $files : files,
            $event : evt
          });
        });
      });
    };
  }])

//1、获取要上传的文件,并且知道要上传到服务器的路径     string s = "Uploads/aaa.txt";   

         

<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

2.服务 上传文件前预览并压缩图片功能

 //2、生成绝对路径     string path = Server.MapPath(s);

 private void Button1_Click(object sender, EventArgs e)
    {
        string path = "Upload/aaa.text";//将类型为TXT的文件命名为aaa.text并存到Upload文件夹里
        string end = Server.MapPath(path);//将虚拟路径转换为物理路径
        FileUpload1.SaveAs(end);          //执行上传命令
    }

然后点击头像开始上传头像,我给头像加了一个手势,点击后跳转到手势方法:

//上传文件预览
angular.module('myServers',[])
  .factory('fileReader', ['$q', '$log', function($q, $log) {
    var dataURItoBlob = function(dataURI) { 
      // convert base64/URLEncoded data component to raw binary data held in a string 
      var byteString; 
      if (dataURI.split(',')[0].indexOf('base64') >= 0) 
        byteString = atob(dataURI.split(',')[1]); 
      else 
        byteString = unescape(dataURI.split(',')[1]); 
      // separate out the mime component 
      var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]; 
      // write the bytes of the string to a typed array 
      var ia = new Uint8Array(byteString.length); 
      for (var i = 0; i < byteString.length; i++) { 
        ia[i] = byteString.charCodeAt(i); 
      } 
      return new Blob([ia], { 
        type: mimeString 
      }); 
    }; 
    var onLoad = function(reader, deferred, scope,file) {
      return function() {
        scope.$apply(function() {
           var img = new Image();
          //前端压缩图片
          img.onload = function(){ 
            //resize the image using canvas 
            var canvas = document.createElement("canvas"); 
            var ctx = canvas.getContext("2d"); 
            var width = img.width; 
            var height = img.height; 
            var MAX_WIDTH = width>2500 ? width/2 : 2500; 
            var MAX_HEIGHT = height>2500 ? height/2 : 2500;
            if (width > height) { 
              if (width > MAX_WIDTH) { 
                height *= MAX_WIDTH / width; 
                width = MAX_WIDTH; 
              } 
            } else { 
              if (height > MAX_HEIGHT) { 
                width *= MAX_HEIGHT / height; 
                height = MAX_HEIGHT; 
              } 
            }
            canvas.width = width ; 
            canvas.height = height; 
            ctx.drawImage(img, 0, 0, width, height); 
            var dataURL = canvas.toDataURL('image/jpeg', 1);
            var blob = dataURItoBlob(dataURL); 
            if(blob.size > 2000 * 1024){
              dataURL = canvas.toDataURL('image/jpeg', .2);
            }else if(blob.size > 1000 * 1024){
              dataURL = canvas.toDataURL('image/jpeg', .5);
            }else{
              dataURL = canvas.toDataURL('image/jpeg', .8);
            }
            blob = dataURItoBlob(dataURL);
            deferred.resolve(blob);
          }
          img.src = URL.createObjectURL(file);
        });
      };
    };
    var onError = function(reader, deferred, scope) {
      return function() {
        scope.$apply(function() {
          deferred.reject(reader.result);
        });
      };
    };
    var onProgress = function(reader, scope) {
      return function(event) {
        scope.$broadcast("fileProgress", {
          total: event.total,
          loaded: event.loaded
        });
      };
    };
    var getReader = function(deferred, scope, file) {
      var reader = new FileReader();
      reader.onload = onLoad(reader, deferred, scope,file);
      reader.onerror = onError(reader, deferred, scope);
      reader.onprogress = onProgress(reader, scope);
      return reader;
    };
    var readAsDataURL = function(file, scope) {
      var deferred = $q.defer();
      var reader = getReader(deferred, scope,file);
      reader.readAsDataURL(file);
      return deferred.promise;
    };
    return {
      readAsDataUrl: readAsDataURL
    };
  }]);

 //3、上传     FileUpload1.SaveAs(path);

本文由必威发布于必威-编程,转载请注明出处:目的是点击按钮上传文件betway体育app:,然后初

TAG标签:
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。