ASP.net;sharepoint
c# moss 程序人生

MOSS的RTF字段的上传图片存在缺陷,上传图片必须填写站点上的图片Url才能上传,这样用户操作起来很不方便,为了方便用户使用,你可以自定义RTF类型的字段,也可以重写上传图片的对话框。

下面介绍一下重写上传图片对话框的操作。

 

1、修改form.js

将原来的的图片上传对话框替换掉。

form.js文件中找到RTE_ModalDialog方法,将/_layouts/RteDialog.aspx 替换成 /_layouts/Sinopec/AppPages/UploadFile.aspx

2、新建上传页面

编写UploadFile.aspx页面,将图片上传到共享文档中,把图片的路径返回给RTF字段的编辑框。

 

UploadFile.aspx的前台页面如下:

<%@ Assembly Name="Microsoft.SharePoint.ApplicationPages" %>

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UploadFile.aspx.cs" Inherits="ModifyRTFImage.AppPage.UploadFile,ModifyRTFImage, Version=1.0.0.0, Culture=neutral, PublicKeyToken=956dc7e41e8e7b7d" %>

<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Import Namespace="Microsoft.SharePoint" %>

 

<script src="/_layouts/<%=System.Threading.Thread.CurrentThread.CurrentUICulture.LCID%>/core.js"></script>

<script src="/_layouts/<%=System.Threading.Thread.CurrentThread.CurrentUICulture.LCID%>/form.js"></script>

<html dir="<SharePoint:EncodedLiteral runat='server' text='<%$Resources:wss,multipages_direction_dir_value%>' EncodeMethod='HtmlEncode'/>">

<!--TOOLBAR_EXEMPT-->

<head>

<base target="_self" />

<SharePoint:CssLink ID="CssLink1" runat="server"/>

<title><asp:Literal id="PageTitle" runat="server"/></title>

<style type="text/css">

body

{

margin-left:10px;

}

input

{

font-family:Verdana;

font-size:8pt;

}

td

{

font-family:Verdana;

font-size:8pt;

}

button

{

font-family:Verdana;

font-size:8pt;

width:<asp:Label id="ButtonWidth" text="<%$Resources:wss,RteDialog_ButtonWidth%>" runat="server"/>;

}

</style>

<script language="JavaScript">

<!--

function onWindowKeyDownHandler()

{

if (event.keyCode == 27)

{

window.close();

}

}

function onLoadHandler()

{

document.pageName =

"<asp:Literal id="PageName" runat="server"/>";

var arg = window.dialogArguments;

if (document.pageName == "CreateLink" &&

arg != null)

{

//StrFirst.value = arg.text;

document.allowRelativeLinks = arg.allowRelativeLinks;

}

RTE_DialogResize();

}

function ok_OnClick()

{

var arr = new Array();

var valid = 1;

arr[0] = "Test";//StrFirst.value;

//code

 

arr[1] = document.getElementById('lblUrl').innerText; // StrSecond.value;

if (document.pageName == "InsertTable")

{

if (arr[0] < 1 ||

arr[1] < 1 ||

isNaN(Number(arr[0])) ||

isNaN(Number(arr[1])))

{

alert("<SharePoint:EncodedLiteral runat='server' text='<%$Resources:wss,RteDialog_RowColValid%>' EncodeMethod='EcmaScriptStringLiteralEncode'/>");

valid = 0;

}

if (valid && arr[0] * arr[1] > 625)

{

alert("<SharePoint:EncodedLiteral runat='server' text='<%$Resources:wss,RteDialog_RowColLimit%>' EncodeMethod='EcmaScriptStringLiteralEncode'/>");

valid = 0;

}

}

if (document.pageName == "CreateLink")

{

if (!IsSafeHrefAlert(arr[1], document.allowRelativeLinks))

{

valid = false;

}

}

if (valid)

{

window.returnValue = arr;

window.close();

}

}

-->

</script>                

</head>

<body class="ms-BuilderBackground" onkeydown="onWindowKeyDownHandler();" onload="onLoadHandler();">

<center>

<form runat="server">

    <table style="border-top: none;" cellspacing="5" id="DialogTable">

        <tr style="display:none">

    <td><label for="StrFirst"><asp:Label id="FirstCaption" runat="server"/></label></td>

    <td><input id="StrFirst" type="text" size=30 maxlength=1024></input></td>

</tr>

<tr style="display:none">

    <td><label for="StrSecond"><asp:Label id="SecondCaption" runat="server"/></label></td>

    <td><input id="StrSecond" type="text" size=30 maxlength=1024></input></td>

</tr>

<tr>

    <td><label>Address:</label></td>

    <td>

        <table>

            <tr>

                <td><asp:FileUpload ID="uploadPic" runat="server" /></td>

                <td><asp:Button ID="Button1" runat="server" Text="上传" onclick="Button1_Click" /></td>

              

            </tr>

            <tr>

                <td>

                图片地址:

                </td>                               

                <td><asp:Label id="lblUrl" runat="server"/></td>                       

            </tr>

        </table>

    </td>

</tr>

<tr><td align="<SharePoint:EncodedLiteral runat='server' text='<%$Resources:wss,multipages_direction_right_align_value%>' EncodeMethod='HtmlEncode'/>" colspan="2">

    <button id="OKButton" onclick="ok_OnClick()" class="ms-ButtonHeightWidth" type="submit"><asp:Label id="OkButton" text="<%$Resources:wss,RteDialog_OK%>" runat="server"/></button>

&nbsp;

<button id="CancelButton" class="ms-ButtonHeightWidth" onclick="window.close();"><asp:Label id="CancelButton" text="<%$Resources:wss,RteDialog_Cancel%>" runat="server"/></button>

</td></tr>

</table>

</form>

</center>

<br/>

</body>

</html>

 

UploadFile.aspx的后台代码如下:

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.IO;

using Microsoft.SharePoint;

 

namespace ModifyRTFImage.AppPage

{

    public partial class UploadFile : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

 

        }

 

        protected void Button1_Click(object sender, EventArgs e)

        {

            if (this.uploadPic.HasFile)

            {

                //获取上传文件的信息

                string name = uploadPic.PostedFile.FileName;

 

                string fileContentType = uploadPic.PostedFile.ContentType;

 

 

                //将图片写到文档库中

                if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg")

                {

                    using (SPWeb web = SPContext.Current.Web)

                    {

                        try

                        {

                            SPDocumentLibrary listImage = (SPDocumentLibrary)web.Lists[new Guid("32DEEFEE-40AD-47A0-B2CB-50C56F76B88D")];

                            string folderUrl = listImage.RootFolder.ServerRelativeUrl + "/" + name.Split('\\')[name.Split('\\').Length - 1];

                            Stream fileNew = new FileStream(name, FileMode.Open);

                            if (!web.AllowUnsafeUpdates)

                                web.AllowUnsafeUpdates = true;

                            listImage.RootFolder.Files.Add(folderUrl, fileNew, true);

 

                            this.lblUrl.Text = folderUrl;

                        }

                        catch { }

                        finally

                        {

                            web.AllowUnsafeUpdates = false;

                        }

                    }

                }

            }

        }

    }

}

 

posted on 2010-04-23 15:06  sumh  阅读(672)  评论(0编辑  收藏  举报