• 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
  • 相关阅读:
    获得随机N位数不重复数字
    springboot配置swagger
    Java开发中的23种设计模式详解(转)
    英文对应解释
    python实现ping命令语音播报联通性V1.0.0.1
    python实现ping命令语音播报联通性V1.0
    python实现ping命令语音播报联通性
    eclipse更新支持tomcat-8.0以上
    网管---------Windows杀死后台进程查询等脚本
    网管-windows磁盘检查修复
  • 原文地址:https://www.cnblogs.com/52life/p/3347210.html
Copyright © 2020-2023  润新知