如何在 ItemAdding 事件中获取 SharePoint ItemEventReciever 中的文件详细信息?

发布于 2024-07-29 13:28:03 字数 632 浏览 11 评论 0原文

我希望这样的东西能够工作,但 ListItem、BeforeProperties、AfterProperties 都是 null/空。 我需要文件名和文件内容。

public class MyItemEventReceiver : SPItemEventReceiver {
    public MyItemEventReceiver() {}
    public override void ItemAdding(SPItemEventProperties properties) {
        SPListItem item = properties.ListItem;
        bool fail = item.File.Name.Equals("fail.txt");
        if (fail) {
            properties.ErrorMessage = "The file failed validation";
            properties.Cancel = true;
        }
    }
}

我无法使用 ItemAdded,因为它是异步的,而我需要同步,我可能会阻止上传并向用户显示消息。

任何建议,将不胜感激。 例如,是否可以覆盖Upload.aspx?

I would expect something like this to work but the ListItem, BeforeProperties, AfterProperties are all null/empty.
I need the file name and file content.

public class MyItemEventReceiver : SPItemEventReceiver {
    public MyItemEventReceiver() {}
    public override void ItemAdding(SPItemEventProperties properties) {
        SPListItem item = properties.ListItem;
        bool fail = item.File.Name.Equals("fail.txt");
        if (fail) {
            properties.ErrorMessage = "The file failed validation";
            properties.Cancel = true;
        }
    }
}

I can't use ItemAdded as it is asynchronous and I need to be synchronous, I may prevent the upload and display a message to the user.

Any suggestions would be appreciated. For example, is it possible to override the Upload.aspx?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薯片软お妹 2024-08-05 13:28:03

您可以使用 HttpContext 检索应包含上传文件的 HttpFileCollection。 这仅适用于通过 Web UI 上传的单个文件。 上传多个文件或直接从 Office 保存不会创建 HttpContext。 尝试这样的事情:

private HttpContext context;

public MyItemEventReceiver() {
    context = HttpContext.Current;
}

public override void ItemAdding(SPItemEventProperties properties) {
    HttpFileCollection collection = context.Request.Files;
    foreach (String name in collection.Keys) {
        if (collection[name].ContentLength > 0) {
            // Do what you need with collection[name].InputStream
        }
    }
}

You can use the HttpContext to retrieve the HttpFileCollection which should contain the uploaded files. This will only work for individual file uploads through the web UI. Doing multiple file uploads, or saving directly from Office won't create an HttpContext. Try something like this:

private HttpContext context;

public MyItemEventReceiver() {
    context = HttpContext.Current;
}

public override void ItemAdding(SPItemEventProperties properties) {
    HttpFileCollection collection = context.Request.Files;
    foreach (String name in collection.Keys) {
        if (collection[name].ContentLength > 0) {
            // Do what you need with collection[name].InputStream
        }
    }
}
爺獨霸怡葒院 2024-08-05 13:28:03

请注意后缀“添加”。 它将为空,因为它尚未添加。 尝试使用-“添加”。

编辑:我相信有一个“AfterProperties 而不是属性对象,你可以在某个地方抓住,我现在不在门外,但我确信你可以在谷歌上进行一些挖掘以找到抛出的相关方法。

Notice the suffix -"adding." It's going to be null because it wasn't added yet. Try using -"added."

EDIT: I believe there's an "AfterProperties rather than properties object you can grab somewhere, i'm out the door at the moment, but i'm sure you can do some digging on google to find the related method getting thrown.

白鸥掠海 2024-08-05 13:28:03

正如 Janie 所写,此事件在插入之前触发,但您应该能够访问 BeforeProperties,这样您就无法使用 ItemAdded 事件。

在大多数情况下,这会为时已晚,因为 ItemAdding 事件通常用于验证输入。

快乐编码

As Janie wrote this event is triggered before the insert, but you should be able to access the BeforeProperties so you don't have the use the ItemAdded event.

That would in most cases be to late as the ItemAdding event is commonly used to validate the input.

Happy coding

各自安好 2024-08-05 13:28:03

可以使用属性来检索文件名(您可以使用一些属性)。 SPItemEventProperties.BeforeUrl 包含此内容。

无法检索文件的内容,因为 SPItemEventProperties。 该文件尚未写入数据库,仅存在于用户连接的服务器的内存中。 因此不幸的是不能使用标准方法。

It is possible to retrieve the filename by using a property (there are a few you can use). SPItemEventProperties.BeforeUrl contains this.

It is not possible to retrieve the contents of the file as this is not provided by any member of SPItemEventProperties. The file has not been written to the database yet and exists only in the memory of the server the user is connected to. Therefore unfortunately standard methods cannot be used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文