zoukankan      html  css  js  c++  java
  • SharePoint Attachement操作代码

    下载文件

    如果下载其它类别的文件: 

     SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
                        {
                            using (SPWeb web = currentSite.OpenWeb(SPContext.Current.Web.ID))
                            {
                                string filename = Request.QueryString["FileName"].ToString();
                                string ItemID = Request.QueryString["ID"].ToString();

                                web.AllowUnsafeUpdates = true;
                                string strContentType = "";

                                SPList list = web.Lists["TestList"];
                                SPFolder folder = web.Folders["Lists"].SubFolders["TestList"].SubFolders["Attachments"].SubFolders[ItemID.ToString()];

                                string url = folder.Url + "/" + filename.ToString();
                                SPFile tempFile = web.GetFile(url);
                                string[] file = filename.Split('.');
                                int a = file.Length;

                                //Get the extension of File.
                                byte[] obj = (byte[])tempFile.OpenBinary();

                                // Get the extension of File to determine the file type
                                string casestring = "";
                                casestring = file[a - 1].ToString();
                                switch (casestring)
                                {
                                    case "txt":
                                        strContentType = "text/plain";
                                        break;
                                    case "htm":
                                        strContentType = "text/html";
                                        break;
                                    case "html":
                                        strContentType = "text/html";
                                        break;
                                    case "rtf":
                                        strContentType = "text/richtext";
                                        break;
                                    case "jpg":
                                        strContentType = "image/jpeg";
                                        break;

                                    case "jpeg":
                                        strContentType = "image/jpeg";
                                        break;
                                    case "gif":
                                        strContentType = "image/gif";
                                        break;
                                    case "bmp":
                                        strContentType = "image/bmp";
                                        break;
                                    case "mpg":
                                        strContentType = "video/mpeg";
                                        break;
                                    case "mpeg":
                                        strContentType = "video/mpeg";
                                        break;
                                    case "avi":
                                        strContentType = "video/avi";
                                        break;
                                    case "pdf":
                                        strContentType = "application/pdf";
                                        break;
                                    case "doc":
                                        strContentType = "application/msword";
                                        break;
                                    case "dot":
                                        strContentType = "application/msword";
                                        break;
                                    case "csv":
                                        strContentType = "application/vnd.msexcel";
                                        break;
                                    case ".xls":
                                        strContentType = "application/vnd.msexcel";
                                        break;
                                    case ".xlt":
                                        strContentType = "application/vnd.msexcel";
                                        break;
                                    default:
                                        strContentType = "application/octet-stream";
                                        break;
                                }
                                Response.ClearContent();
                                Response.ClearHeaders();
                                Response.AppendHeader("Content-Disposition""attachment; filename=" + filename.ToString());
                                Response.ContentType = strContentType;

                                //Check that the client is connected and has not closed the connection after the request
                                if (Response.IsClientConnected)
                                {
                                    Response.BinaryWrite(obj);
                                }
                                Response.Flush();
                                Response.Close();
                            }
                        }
                    });
                }
                catch (Exception ex)
                {

                }
    View Code
  • 相关阅读:
    Shell脚本实现用户数据导入
    Sping Cloud 微服务框架学习
    Redis学习笔记
    HTML+CSS实现简单三级菜单
    CSS Float浮动所带来的奇怪现象
    CSS如何清除浮动流的多种方案
    ECMAScript/JS 基础知识讲解
    Python的生成器
    Python包的导入说明
    Paramiko模块简单使用
  • 原文地址:https://www.cnblogs.com/52life/p/3347210.html
Copyright © 2011-2022 走看看