简体   繁体   中英

Need simple example of file upload

I need a very simple example. I simply want to use ng-file-upload to get the file body into an object field defined as a binary array in my view controller. I don't need to upload the file to a file system on a server. The doc for ng-file-upload is quite complex. Thanks in advance.

Here is what I have so far:

    <form name="form" ng-submit="vm.save()">                    
    <div class="widget-content user">
       Business Logo: <input type="file" 
    ngf-select ng-model="vm.logoFile" accept="image/*" ngf-max-size="2MB"
    required autofocus placeholder="Logo image file" />
   </div>
   <div class="app-command-bar">
   <button type="submit" class="btn btn-success navbar-btn"
        ng-disabled="isSaveDisabled()"
        title="SignUp">
    <i class="icon-save"></i> Sign Up
   </button>&nbsp;&nbsp;

and in my controller upon submit:

     if (vm.logoFile != null) {
            log("logoFile" + vm.logoFile);
            log("logoFile type: " + vm.logoFile.type);
            log("logoFile name: " + vm.logoFile.name);
            vm.logoFile.upload = Upload.http({
                url: "",
                headers: {
                    'Content-Type': vm.logoFile.type
                },
                data: vm.logoFile,
            });
            log("Attempting upload");
            // we are trying to post the page again; no can do
            vm.logoFile.upload.then(function (response) {
                vm.logoFile.result = response.data;  //supposed to be the file binary at this point
                log("File result: " + vm.logoFile.result);
            });

I'm having a concept problem regarding the upload location. I'm using "" in the URL which posts to my page, but I'm getting 405 Method not Allowed . Seems to me I'm trying to repost this page. I'm trying to just get the binary data from the file; I don't want to upload the file to a server. Any suggestions? Thanks.

ok..upon further thought. I need an entrypoint on my web app to accept the upload requests. My web app so far is a pure javascript SPA. What kind of entrypoint do I need to add to the application? Thanks for any help.

I have gone another way, but am posting as an answer here for those poor slobs like me that need a simple solution. I just added an Angular service that has this function:

     function uploadfile(file) {
        var defer = $q.defer();
        var reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function (event) {
            defer.resolve(event.target.result);
        };
        return defer.promise;
    };

When the file bytes come back I just parse the type and the pure base64string into the fields (using .split(",")) I needed to post to my database on the backend.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM