从sharepoint下载文件出错

Zine_Alone 2009-05-12 10:47:01
当我用流读取sharepoint上文件时,老是没能获取到正确大小的文件,而且在读取pdf文件时,会出现文件损坏的错误...下面是代码供大家参考(在测试环境是没问题的,可以正常运行,部署到客户那后就出现这个问题了,两者唯一不同的是客户那是用了SSL).


public void OpenAttachment(string RootFolder, string SubFolder, string FileName, string DocName)
{
string url = "";
try
{
//Get the extension of File.
string[] fext = DocName.Split('.');

// Get the extension of File to determine the file type
string casestring = "";
if (fext.Length > 1)
{
casestring = fext[fext.Length - 1];
}

//set the content type of file according to extension
int flag = 0;

SPWeb web = SPContext.Current.Web;
web.AllowUnsafeUpdates = true;
string contentType = "";

url = SPContext.Current.Web.Url + "/" + RootFolder + "/" + SubFolder + "/" + FileName + "." + casestring;

SPFile tempFile = web.GetFile(url);

byte[] obj = (byte[])tempFile.OpenBinary();

switch (casestring)
{

case "txt":

contentType = "text/plain";

break;

case "htm": contentType = "text/html";

break;

case "html": contentType = "text/html";

break;

case "rtf": contentType = "text/richtext";

break;

case "jpg": contentType = "image/jpeg";

break;

case "jpeg": contentType = "image/jpeg";

break;

case "gif": contentType = "image/gif";

break;

case "bmp": contentType = "image/bmp";

break;

case "mpg": contentType = "video/mpeg";

break;

case "mpeg": contentType = "video/mpeg";

break;

case "avi": contentType = "video/avi";

break;

case "pdf": contentType = "application/pdf";

break;

case "doc": contentType = "application/msword";

break;

case "dot": contentType = "application/msword";

break;

case "csv": contentType = "application/vnd.msexcel";

break;

case ".xls": contentType = "application/vnd.msexcel";

break;

case ".xlt": contentType = "application/vnd.msexcel";

break;

default: contentType = "application/octet-stream";

break;

}

HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename= " + DocName);
HttpContext.Current.Response.ContentType = contentType;

//Check that the client is connected and has not closed the connection after the request

if (HttpContext.Current.Response.IsClientConnected)
HttpContext.Current.Response.BinaryWrite(obj);

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

catch (Exception ex)
{
Response.Write(ex.ToString());
}
}


...全文
234 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ak8800 2009-06-03
  • 打赏
  • 举报
回复
sharepoint 上传的文件不都是已附件的形式存在的吗,附件有一个随机生成的文件夹,写路径的时候需要把这个路径考虑清楚了!
kaukiyou 2009-05-18
  • 打赏
  • 举报
回复
看不出什么问题.
这个方法的目的是什么, sharepoint文件本来就可以下载的,为什么新写这个方法?
消火栓2019 2009-05-12
  • 打赏
  • 举报
回复
是不是文件太大了超过最大限制了,config里设置一下呢
Zine_Alone 2009-05-12
  • 打赏
  • 举报
回复
应该不是这个问题引起的,还有别的可能吗?
开发工具扩展到自动保存脚本/css文件SharePoint。 开发人员工具扩展,可将脚本/ css文件自动保存到SharePoint。 这是更改和测试SharePoint App内容文件的最快方法。 另外,这可能是修复某些js / css错误的好机会,而无需使用Visual Studio部署脚本或提供新的应用程序包。 主要特征:* Cntrl + s强制重写js或css。 *允许从主机Web修改SharePoint 2013 App内容*如果“要求文档必须先签出才能进行编辑”选项为True,则允许自动签出js / css文档; *允许通过在控制台中执行SPush(typeOrUrl)快速创建示例js和css文件(请参见示例视频或访问下面的GitHub链接)0.63 *修复了保存在非根网站集中的错误。 0.64 *解决了从主机网络编辑应用程序脚本时相对URL的问题。 0.65 *添加了DevTools错误登录。 0.66 * JSLink保存问题已修复。 0.67 *解决了保存带有注释和其他规范的文件的问题; *'style%20library'解码问题已修复。 0.68 *内部js修复。 1.0 *实现了SPush(typeOrUrl)帮助文件的创建方法; *添加了“库URL”配置。 1.0.1 *徽标已更改1.0.2 *修复了子网上的快速摘要SPush('js')的问题; *从OOB SharePoint加载css / js方法中删除了依赖性。 在SharePoint 2010+(本地和O365 SharePoint Online)下进行了测试。 在此处查找GIT存储库:https://github.com/wideserg/CE/tree/master/SPush 支持语言:English

3,242

社区成员

发帖
与我相关
我的任务
社区描述
企业开发 SharePoint
社区管理员
  • SharePoint社区
  • 霖雨
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧