使用php curl将zip文件上传到远程服务器
问题描述:
正在执行以下操作: 1)客户端使用API调用从我们的服务器请求ZIP文件。 2)他提供一个回调url,它是API请求中的一个aspx程序。 3)我们创建ZIP文件,使用php CURL脚本将ZIP文件上传到他的aspx程序中。使用php curl将zip文件上传到远程服务器
问题是,当文件上传到他的服务器上时,ZIP文件格式会更改(SFX Zip存档)。如果使用简单的php脚本将相同的文件上传到我们的服务器,则格式保持不变。我们不确定这个问题与我们使用CURL上传文件的方式有关,还是与客户端将ZIP文件保存在其服务器上的方式相同。
卷曲代码如下:
$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);
客户端用于保存数据所提供的码的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();
}
}
感谢。
答
你保存SavePost
不保存任何内容? 您尝试从this.Page.Request.InputStream中读取,请参阅FileUpload to FileStream如何转换它。
btw另请参阅How to read inputstream from HTML file type in C# ASP.NET without using ASP.NET server side control。这说明你也可以使用(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)));
}