【问题标题】:Google Apps Script To POST PDF and IMAGE data to external Apache serverGoogle Apps 脚本将 PDF 和 IMAGE 数据发布到外部 Apache 服务器
【发布时间】:2016-08-09 06:46:21
【问题描述】:

我需要一种方法从我们的 google 驱动器中获取图像文件和 pdf 文件,并以编程方式传输到我们的 apache 服务器。

如果我使用标准的 HTML5 表单来执行上传,我的 PHP 工作得很好,这让我相信它是正确编写的。 (我可能错了)。当我使用 google apps 脚本执行上传时,出现以下错误。

[06-Aug-2016 16:52:18 America/Chicago] PHP 警告:multipart/form-data POST 数据在第 0 行的 Unknown 中缺少边界

如果我将内容类型参数更改为以下

"contentType" : "multipart/form-data boundary=--",

这会使警告消失,但似乎没有文件到达目标服务器。从我自己的研究来看,我相当确定我需要设置这个边界参数才能让所有东西都能很好地发挥作用。我也相信默认是双连字符。但在这种情况下似乎不正确。

感谢任何帮助我在这里有点超出我的深度。如果需要更多信息,请告诉我,我会尽快回复。

谢谢,

Chris Pfannkuch - 俄勒冈州排水沟

///////////////////////////////////////////////////////////////////
// Google Apps Script - Code.gs
function sendHttpPost() 
{
   var fileBlob = DriveApp.getFileById("FILEIDHERE").getBlob();

   var payload =
   {
     "fileAttachment": fileBlob
   };

   // FROM Google Apps Script Documentation.
   // Because payload is a JavaScript object, it will be interpreted as
   // an HTML form. (We do not need to specify contentType; it will
   // automatically default to either 'application/x-www-form-urlencoded'
   // or 'multipart/form-data')

   // FROM PHP.net documentation.
   // Be sure your file upload form has attribute enctype="multipart/form-data"
   // otherwise the file upload will not work.                 

  // When I have omitted this parameter I get multiple warnings in my error log
  // on my destination server.

   var options =
   {
     "method" : "post",
     "contentType" : "multipart/form-data",
     "payload" : payload
   };

   UrlFetchApp.fetch("http://OURDOMAIN.com/SOMEFOLDER/handleHttpPost.php", options);
}
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
//PHP - handleHttpPost.php
<?php
    $uploaddir = 'SOMEDIR/';
    $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

    echo '<pre>';
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) 
    {
        echo "File is valid, and was successfully uploaded.\n";
    } 
    else 
    {
        echo "Failed.\n";
    }

    echo 'Here is some more debugging info:';
    print_r($_FILES);

    echo "</pre>";
?>
///////////////////////////////////////////////////////////////////

编辑:这是我一直在查看的一些帖子数据。

///////////////////////////////////////////////////////////////////
// POST From standard HTML5 WebForm, works great to upload the pdf file.
http://posttestserver.com/files/2016/08/08/f_12.45.381108844561
///////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////
// POST From UrlFetchApp call.
http://www.posttestserver.com/data/2016/08/08/12.56.43188879461
///////////////////////////////////////////////////////////////////

UrlFetch 调用似乎没有将该文件视为已上传文件。

== Multipart File upload. ==
Received 0 file(s)

当我使用表单时,它确实会看到文件。

== Multipart File upload. ==
Received 1 file(s)
 0: posted name=fileAttachment
    name: OfficeClerk.pdf
    type: application/pdf
    error: 0
    size: 752176

【问题讨论】:

    标签: php google-apps-script


    【解决方案1】:

    今天解决了我的问题。我需要包括看似无用的 fieldOne 和 fieldTwo。一旦我做到了,这一切就开始很好地协同工作。我是个白痴,因为我在早期调试过程中的某个时候删除了它们。

    var payload =
       {
         "fieldOne" : "value for field one",
         "fieldTwo" : "value for field two",
         "fileAttachment": fileBlob
       };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-17
      • 2018-03-15
      • 1970-01-01
      • 2013-06-25
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      相关资源
      最近更新 更多