简体   繁体   English

使用php curl将zip文件上传到远程服务器

[英]Uploading zip file to remote servers using php curl

Following is being done: 1) Client requests a ZIP file from our server using an API call. 正在执行以下操作:1)客户端使用API​​调用从我们的服务器请求ZIP文件。 2) He provides a call back url which is an aspx program in the API request. 2)他提供了一个回调URL,它是API请求中的一个aspx程序。 3) We create the ZIP file, using php CURL script upload the ZIP file to his aspx program. 3)我们创建ZIP文件,使用php CURL脚本将ZIP文件上传到他的aspx程序。

The issue is, the ZIP file format changes (to SFX Zip archive) when the file gets uploaded on his server. 问题是,当文件上传到他的服务器时,ZIP文件格式更改为(SFX Zip存档)。 If the same file is uploaded to our server using a simple php script the format remains unchanged. 如果使用简单的php脚本将相同文件上传到我们的服务器,则格式保持不变。 We are not sure if the issue is with the way we are uploading the file using CURL or if its with the way the client is saving the ZIP file on his server. 我们不确定问题出在我们使用CURL上载文件的方式还是客户端在其服务器上保存ZIP文件的方式。

The CURL code is as following : CURL代码如下:

$download_file = "/tmp/test.zip";
$callBackUrl = "http://www.remoteurl/theclients_uploadcode.aspx";
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => "@$download_file"));
curl_setopt($ch, CURLOPT_URL, $callBackUrl);
curl_exec($ch);
curl_close($ch);

The client provided the aspx code for saving the data: 客户端提供了用于保存数据的aspx代码:

private void SavePost()
{
   HttpPostedData = ReadFully(this.Page.Request.InputStream);
   Timestamp = DateTime.Now.ToString("MMddyyyy-hhmmss");
   Timestamp = Timestamp.Replace("-", "").Replace(" ", "").Replace(":", "");                      
   if (FileName.ToString() == string.Empty)
   {
    FileName = Timestamp;
   }
   if (FileName.Contains(".zip") == false)
   {
    FileName = FileName + ".zip";
   }
   tempFilePath = (tempFilePath + FileName);
   Response.Write((tempFilePath + ("  HttpPostedData:" + HttpPostedData)));
}

 public static byte[] ReadFully(Stream input)
    {

        byte[] buffer = new byte[16 * 1024];
        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            return ms.ToArray();
        }

    }

Thanks in advance. 提前致谢。

you save SavePost don't save anything? 您保存SavePost不保存任何内容? you try to read from this.Page.Request.InputStream, see FileUpload to FileStream how to convert it. 您尝试从this.Page.Request.InputStream读取,请参阅FileUpload到FileStream的转换方法。

btw see also How to read inputstream from HTML file type in C# ASP.NET without using ASP.NET server side control . btw另请参见如何在不使用ASP.NET服务器端控件的情况下从C#ASP.NET中的HTML文件类型读取inputstream This shows you can also use (c#): 这表明您也可以使用(c#):

HttpPostedFile file = Request.Files["file"];
if (file != null && file.ContentLength )
{
    string fname = Path.GetFileName(file.FileName);
    file.SaveAs(Server.MapPath(Path.Combine("~/App_Data/", fname)));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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