Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使字段类型的输入以base64而不是二进制格式发送数据_Javascript_Html_Html4 - Fatal编程技术网

Javascript 使字段类型的输入以base64而不是二进制格式发送数据

Javascript 使字段类型的输入以base64而不是二进制格式发送数据,javascript,html,html4,Javascript,Html,Html4,我正在用html4编写一个简单的上传表单。这通常是一个非常简单的过程。我遇到的问题是,对于映像数据,服务器需要base64,并且将处理我作为base64发送的任何内容,从而导致映像损坏。我可以为我的表单设置某种属性吗?如果没有,我可以设置一些javascript,在将数据发送到服务器之前将其编码到base64中吗?这里有一个指向文档丰富的base64编码器/解码器的链接。我修改了这段代码(见下文),因此它不需要“core”类 ///BEGIN\u DOC(base64)。方法(解码) /// /

我正在用html4编写一个简单的上传表单。这通常是一个非常简单的过程。我遇到的问题是,对于映像数据,服务器需要base64,并且将处理我作为base64发送的任何内容,从而导致映像损坏。我可以为我的表单设置某种属性吗?如果没有,我可以设置一些javascript,在将数据发送到服务器之前将其编码到base64中吗?

这里有一个指向文档丰富的base64编码器/解码器的链接。我修改了这段代码(见下文),因此它不需要“core”类

///BEGIN\u DOC(base64)。方法(解码)
///
//方法RETURNTYPE base64.decode(字符串inp[,枚举outType[,bool-safe[,bool-lax]]))
//
//将输入数据编码为base64字符串。
//
//函数参数:
//字符串inp:要解码的base64编码数据字符串。
//枚举输出类型可选。此参数指定输出的类型并确定
//如何解释输入数据:
//0-二进制数据;创建字节数组(默认)
//1-8位字符串,假设以inp编码的1字节字符
//2-16位(UniCode)字符串,假定为2字节
//用inp编码的字符
//如果将2传递给函数,则为base64字符数
//如果为奇数,则返回null值。
//bool-safe可选。如果此参数设置为true,则标准base64
//字符集将替换为修改后的版本,其中
//将字符“+”和“/”替换为“-”和“\u1”,
//另外,为了避免文件系统出现问题
//否则可能在某些系统上发生的命名。
//默认情况下,此参数的值假定为
//错。
//bool-lax可选。如果设置为true,函数将跳过所有输入字符
//无法处理。这也包括字符“=”,如果
//字符串前至少跟有一个不同的字符
//结束。但是,如果跳过不可行的字符等于一个数字
//允许的base64字符(不是4的多个),
//这被视为错误,返回null。
//如果lax设置为false(默认值),则返回null
//每当发现不可行的字符时。
//此参数的目的是在某些情况下提供支持
//其中数据已进行base64编码,之后由
//电子邮件中插入了其他一些软件,例如“\r\n”。
//交换。
//返回值:函数的处理结果值存储在字符串或
//返回前的字节数组,具体取决于值
//指定给类型参数。在每种情况下,值
//如果没有发生错误,可能为空,但不为null。
//错误:每当发生错误时,返回null。参数值
//以上未定义的被视为错误。
//
///完
base64.decode=函数(inp、outType、safe、lax){
//做一些参数检查
if(arguments.length<1)返回null;
if(arguments.length<2)outType=0;//默认情况下生成字符数组
如果(outType!=0&&outType!=1&&outType!=2)返回null;
if(arguments.length>=3&&safe!=true&&safe!=false)返回null;
var sEnc=(arguments.length>=3&&safe)?this.encStringS:this.encString;//选择编码字符集
if(arguments.length>=4&&lax!=true&&lax!=false)返回null;
var aDec={};//创建用于解码的关联数组
对于(var p=0;p>(2*(3-j));//转换最左边的8位并将它们附加到字符串中
}

l&=~(0xff问题是它使用画布,据我所知,它只是html 5,我需要能够在html 4中完成所有工作。这可能很难。甚至不可能。JS不是为处理文件而设计的。如果你不能使用画布或在服务器端使用画布,可能flash将是解决方案。我有一个base64 javascript类,我只是有一个很难将文件内容放入javascript
/// BEGIN_DOC(base64).METHOD(decode)
///
// method RETURNTYPE base64.decode(String inp [, enum outType [, bool safe [, bool lax]]])
//
// Encode input data into a base64 character string.
//
// Function arguments:
//     String inp:           base64 encoded data string to be decoded.
//     enum outType          Optional. This parameter specifies the type of the output and determines
//                           how the input data is to be interpreted.:
//                             0  - binary data; create a byte array (default)
//                             1  - 8-bit character string, assuming 1-byte characters encoded in inp
//                             2  - 16-bit (UniCode) character string, assuming 2-byte 
//                                  characters encoded in inp
//                           If 2 is passed to the function, but the number of base64 characters
//                           is odd, a value of null is returned.
//     bool safe             Optional. If this parameter is set to true, the standard base64 
//                           character set is replaced with a modified version where
//                           the characters '+' and '/' are replaced with '-' and '_',
//                           repectively, in order to avoid problems with file system
//                           namings which otherwise could occur on some systems.
//                           By default, the value of this argument is assumed to be
//                           false.
//     bool lax              Optional. If set to true, the function skips all input characters which
//                           cannot be processed. This includes the character '=', too, if
//                           it is followed by at least one different character before the string
//                           ends. However, if skipping infeasible characters amounts to a number
//                           of allowed base64 characters which is not amultiple of 4,
//                           this is considered an error and null is returned.
//                           If lax is set to false (the default), null is returned
//                           whenever an infeasible character is found.
//                           The purpose of this parameter is to give support in cases
//                           where data has been base64 encoded and later on was folded by
//                           some other software, e.g. '\r\n\'s have been inserted in email.
//                           exchange.
// Return value:             The function's processing result value is stored in a string or in
//                           a byte array before it is returned, depending on the value 
//                           assigned to the type parameter. In each case, the value
//                           maybe empty but not null if no error occurred.
// Errors:                   Whenever an error occurs, null is returned. Parameter values
//                           not defined above are considered errors.
//
/// END_DOC

base64.decode = function(inp, outType, safe, lax) {

  // do some argument checking
  if (arguments.length < 1) return null;
  if (arguments.length < 2) outType = 0; // produce character array by default
  if (outType != 0 && outType != 1 && outType != 2) return null;
  if (arguments.length >= 3 && safe != true && safe != false) return null;
  var sEnc = (arguments.length >= 3 && safe) ? this.encStringS : this.encString;  // select encoding character set
  if (arguments.length >= 4 && lax != true && lax != false) return null;
  var aDec = {};                // create an associative array for decoding
  for (var p = 0; p < sEnc.length; p++) { // populate array
    aDec[sEnc.charAt(p)] = p;
  }
  var out = (outType == 0) ? [] : '';
  lax = (arguments.length == 4 && lax); // ignore non-base64 characters
  var l = 0;               // work area
  var i = 0;               // index into input
  var j = 0;               // sextett counter
  var c = 0;               // input buffer
  var k = 0;               // index into work area
  var end = inp.length;    // one position past the last character to be processed
  var C = '';
  // check input
  if (lax) {
    var inpS = '';         // shadow input
    var ignore = false;    // determines wether '=' must be counted
    var cnt = 0;
    for (var p = 1; p <= inp.length; p++) {    // check and cleanup string before trying to decode
      c = inp.charAt(end - p);
      if (c == '=') {
        if (!ignore) {
          if (++cnt > 1) ignore = true;
        } else {
          continue;
        }
      } else if (undefined != aDec[c]) { // the character is base64, hence feasible
        if (!ignore) ignore = true;      // no more '=' allowed
        inpS = c + inpS;                 // prepend c to shadow input
      }
    }
    for (var p = 0; p <= cnt; p++) {     // at most cnt '=''s were garbage, a number in 
      if (p == 2) return null;           // [inpS.length, inpS.length + cnt] must be a
      if ((inpS.length + cnt) % 4 == 0) break;  // multiple of 4
    }
    if (inpS.length % 4 == 1) return null;   // must be 0, 2, or 3 for inpS to contain correctly base64 encoded data
    inp = inpS;                          // inp now contains feasible characters only
    end = inp.length;
  } else {
    if (inp.length % 4 > 0) return null;   // invalid length
    for (var p = 0; p < 2; p++) {        // search for trailing '=''s
      if (inp.charAt(end - 1) == '=') {
        end--;
      } else {
        break;
      }
    }
  }
  // convert
  for (i = 0; i < end; i++) {
    l <<= 6;                             // clear space for next sextett
    if (undefined == (c = aDec[inp.charAt(i)])) return null; // lax must be false at this place!
    l |= (c & 0x3f);    // append it
    if (j == 0) {
      j++;
      continue;                          // work area contains incomplete byte only
    }
    if (outType == 2) {
      if (k == 1) {                      // work area contains complete double byte
        out += String.fromCharCode(l >> (2 * (3 - j)));  // convert leftmost 16 bits and append them to string
        l &= ~(0xffff << (2 * (3 - j)));       // clear the 16 processed bits
      }
      k = ++k % 2;
    } else {                             // work area contains complete byte
      if (outType == 0) {
        out.push(l >> (2 * (3 - j)));          // append byte to array
      } else {
        out += String.fromCharCode(l >> (2 * (3 - j))); // convert leftmost 8 bits and append them to String
      }
      l &= ~(0xff << (2 * (3 - j)));           // clear the 8 processed bits
    }
    j = ++j % 4;                           // increment sextett counter cyclically
  }
  if (outType == 2 && k == 1) return null;  // incomplete double byte in work area

  return out;
}