0

正在完成以下工作: 1) 客户端使用 API 调用从我们的服务器请求一个 ZIP 文件。2)他在API请求中提供了一个回调url,它是一个aspx程序。3)我们创建ZIP文件,使用php CURL脚本将ZIP文件上传到他的aspx程序中。

问题是,当文件上传到他的服务器上时,ZIP 文件格式会更改(到 SFX Zip 存档)。如果使用简单的 php 脚本将相同的文件上传到我们的服务器,则格式保持不变。我们不确定问题是否与我们使用 CURL 上传文件的方式有关,或者是否与客户端在其服务器上保存 ZIP 文件的方式有关。

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);

客户端提供了保存数据的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();
        }

    }

提前致谢。

4

1 回答 1

0

你保存SavePost不保存任何东西?您尝试从 this.Page.Request.InputStream 中读取,请参阅FileUpload to FileStream如何转换它。

btw 另请参阅如何在不使用 ASP.NET 服务器端控件的情况下从 C# ASP.NET 中的 HTML 文件类型读取输入流。这表明您也可以使用 (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)));
}
于 2013-04-24T12:56:56.753 回答