2010-08-05 193 views
1

我写了下面的代码从sharepoint下载文件。下载的文件只在某些机器上正常工作。对于其他人来说,它说文件已损坏。问题是MS Office和图像文件,但是PDF没有任何问题。我们发现腐败问题是由于在文件内容的顶部添加了一个十六进制数字。当它被删除时,文件被正确打开。十六进制字符已被描绘出来以字节表示文件大小。为什么这只发生在某些机器上,我们该如何解决?ASP.NET下载的文件损坏

private void DownloadFile() 
{ 
    SPListItem item = GetFileFromSharepoint(); 

    if (item != null) 
    { 
     byte[] bytes = item.File.OpenBinary(); 
     Response.ClearContent(); 
     Response.ClearHeaders(); 
     string fileType = string.Empty; 
     fileType = item["FileFormat"].ToString(); 
     Response.AppendHeader("Content-Disposition", 
           string.Format("attachment; filename= {0}", item["Filename"].ToString().Replace(" ", ""))); 
     Response.ContentType = GetContentType(fileType); 
     //Check that the client is connected and has not closed the connection after the request 
     if (Response.IsClientConnected) 
     { 
      Response.BinaryWrite(bytes); 
     } 
    } 

    Response.Flush(); 
    Response.Close(); 
} 

回答

0

the documentation从,它会出现表明:

如果 大小的文件的是0(零)字节OpenBinary方法失败。

您确定此函数的返回值是否正确?您没有检查bytes阵列的长度。

更新:

也许经过SPOpenBinaryOptions.UnprotectedOpenBinary可能会奏效?