JavaScript:将图像URL转换为文件对象

时间:2015-11-12 23:22:21

标签: javascript ajax image url download

我正在尝试从输入框中的URL获取图像。基本上,我想将图像URL转换为文件对象。

为了进行比较,如果您转到Google图片并选择随机图片,则可以复制图片或图片的网址。

在我的情况下,用户会抓取该网址(例如https://pbs.twimg.com/profile_images/638751551457103872/KN-NzuRl.png)并粘贴到输入框中,然后点击我的“添加图片”按钮。我想在本地访问这个图片文件,我可以从ng-file-upload调用Upload.dataUrl(myImageFile)传递给我的一个函数,该函数会为我上传。

目前,我已经检查过以确保网址是有效的图片,但我不确定如何获取图片(图片网址与图片的对比)。

formSubmit: function(){
      var url = document.forms["imageForm"].elements["urlImage"].value;
      console.log(url);
      if (!$scope.checkURL(url)) {
        console.log("It looks like the url that you had provided is not valid! Please only submit correct image file. We only support these extensions:- jpeg, jpg, gif, png.")
        return(false);
      }
      $scope.testImage(url, function(testURL, result) {
        if (result == "success") {
          // you can submit the form now
          console.log("SUCCESS!");
          //document.forms["submitForm"].submit();

          //!!!---------------------!!!
          //Correct URL link, but not sure how to get the image from URL
          //!!!---------------------!!!

        } 
        else if (result == "error") {
          console.log("The image URL does not point to an image or the correct type of image.");
        } 
        else {
          console.log("The image URL was not reachable.  Check that the URL is correct.");
        }
      });
      return(false); // can't submit the form yet, it will get sumbitted in the callback

    },
    checkURL: function(url){
      return(url.match(/\.(jpeg|jpg|gif|png)$/) != null);
    },
    testImage: function(url, callback, timeout) {
      timeout = timeout || 5000;
      var timedOut = false, timer;
      var img = new Image();
      img.onerror = img.onabort = function() {
          if (!timedOut) {
              clearTimeout(timer);
              callback(url, "error");
          }
      };
      img.onload = function() {
          if (!timedOut) {
              clearTimeout(timer);
              callback(url, "success");
          }
      };
      img.src = url;
      timer = setTimeout(function() {
          timedOut = true;
          callback(url, "timeout");
      }, timeout); 
    }

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

你也不需要太困难 这很简单

/******************************************************************************/
/* Files to Include                                                           */
/******************************************************************************/

#define _XTAL_FREQ 4000000

#include <xc.h>
#include <stdint.h>        /* For uint8_t definition */
#include <stdbool.h>       /* For true/false definition */
#include <htc.h>

#include "system.h"        /* System funct/params, like osc/peripheral config */
#include "user.h"          /* User funct/params, such as InitApp */
#include "pic16f887.h"
/******************************************************************************/
/* User Global Variable Declaration                                           */
/******************************************************************************/

/* i.e. uint8_t <variable_name>; */

/******************************************************************************/
/* Main Program                                                               */
/******************************************************************************/
// CONFIG1
#pragma config FOSC = INTRC_CLKOUT// Oscillator Selection bits (INTOSC oscillator: CLKOUT function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF      // RE3/MCLR pin function select bit (RE3/MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON       // Brown Out Reset Selection bits (BOR enabled)
#pragma config IESO = OFF       // Internal External Switchover bit (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
#pragma config LVP = OFF        // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)

// CONFIG2
#pragma config BOR4V = BOR40V   // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF        // Flash Program Memory Self Write Enable bits (Write protection off)

void interrupt led(void)


{
    PORTA = 0X00;`enter code here`

}


void main(void)
{  
   OSCCON = 0b01110000;

    TRISA = 0X00;
    ANSEL = 0X00;
    ANSELH = 0X00;

    while(1)
    {
        PORTA = 0X03;
        __delay_ms(1000);
        PORTA = 0X01;
        __delay_ms(1000);


    }

}