2012-04-07 114 views
0

谁能帮我,我想自定义,我想验证上传的图片类型图片库自定义上传文件功能

在哪里可以设置我的脚本上传功能?任何人都可以建议?

回答

5

你可能会使用ItemAdding。在ItemAdding事件方法只是通过错误消息比前检查成功上传到Library.if unvalid文档的文档的扩展

你的代码是这样的:

protected string[] ValidExtensions = new string[] { "png", "jpeg", "gif"}; 

    public override void ItemAdding(SPItemEventProperties properties) 
    { 
     string strFileExtension = Path.GetExtension(properties.AfterUrl); 

     bool isValidExtension = false; 

     string strValidFileTypes = string.Empty; 

     using (SPWeb web = properties.OpenWeb()) 
     { 

       foreach (string strValidExt in ValidExtensions) 
       { 
        if (strFileExtension.ToLower().EndsWith(strValidExt.ToLower())) 
        { 
         isValidExtension = true; 
        } 
        strValidFileTypes += (string.IsNullOrEmpty(strValidFileTypes) ? "" : ", ") + strValidExt; 
       } 

     // Here i am going to check is this validate or not if not than redirect to the 
     //Error Message Page. 
       if (!isValidExtension) 
       { 
        properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl; 
        properties.RedirectUrl = properties.WebUrl + "/_layouts/error.aspx?ErrorText=" + "Only " + strValidFileTypes + " extenstions are allowed"; 

       } 

     } 

    } 
+0

Thnx亲爱的everthing是好的....非常感谢你:) – qablan89 2012-04-09 14:00:40

+0

你的欢迎:) – Jigs 2012-04-09 14:02:27

0

你可以使用SPItemEventReceiver您的库并添加您的逻辑为ItemUpdating()和ItemAdding()方法。

+0

很抱歉,但在项目中添加事件接收器,仍然没有上传的图像,会给我空例外 – qablan89 2012-04-07 12:47:56

+0

任何一个忠告? – qablan89 2012-04-07 16:32:43

+0

@ qablan89,如果我的理解正确,您需要在上传前仔细检查扩展程序。因此,作为ItemAdding()方法参数的SPItemEventProperties对象的“属性”包含属性BeforeUrl和AfterUrl。这些属性包含用户上传的文件的名称。 – Tannheuser 2012-04-08 08:09:10

0

你可以尝试创建一个自定义列表模板,在那里替换默认的NewForm.aspxEditForm.aspx页面。这些custom form templates不需要包含与默认图片库模板中相同的用户控件和按钮。您可以创建带有丰富用户界面的Silverlight Web部件来上传图片,例如你想要区分的越多,代码越多,你必须写...

我能想到的一个OOTB解决方案将是一个工作流程,你可以强制每个新的图片运行,但它会是一个过度的对于最终用户...

当然,如果您能够通过使用ItemAdding中的元数据进行验证(如其他人所建议的那样),那将会是一个非常节省时间的方法。

--- Ferda