c# - 将文件流式传输到外部 SharePoint 时的上次修改属性

标签 c# http sharepoint webrequest webresponse

我目前正在使用一项 Windows 服务,该服务可从特定位置移动文件并使它们与 SharePoint 文档库保持同步。

上传/同步/等功能运行良好,但我遇到文件属性问题。上传时(下面的代码示例),文件的 LastModified 属性设置为文件上传的时间。如果我直接将文件复制/粘贴到该目录,则不会出现这种情况。

我研究了在属性上传后仅更改属性的可能性,但这并不理想。从测试来看,这似乎是由于在另一端将流“构建”为新文件引起的?有没有办法随文件一起发送文件属性?

public static string UploadFile(string destUrl, string sourcePath, CredentialCache cc)
{
    try
    {
        Uri destUri = new Uri(destUrl);
        FileStream inStream = File.OpenRead(sourcePath);
        WebRequest req = WebRequest.Create(destUri);
        req.Method = "PUT";
        req.Headers.Add("Overwrite", "F");
        req.Timeout = System.Threading.Timeout.Infinite;
        req.Credentials = cc;
        Stream outStream = req.GetRequestStream();
        byte[] buffer = new byte[32768];
        int read;
        while ((read = inStream.Read(buffer, 0, buffer.Length)) > 0)
        {
            outStream.Write(buffer, 0, read);
        }
        outStream.Flush();
        outStream.Close();
        inStream.Flush();
        inStream.Close();
        WebResponse ores = req.GetResponse();
        ores.Close();
        return "success";          
    } //End Try for Try/Catch of UploadFile()
    catch (Exception ex)
    {
        return ex.Message;
    } //End Try/Catch for UploadFile()
} //End UploadFile()

编辑 - 附加信息

总结一下我在下面的答案中留下的评论:

自从我发布问题后,我还注意到 Sharepoint 将信息列为新信息,即使您直接复制它也是如此,因为它是基于数据库信息(我相信?)。我查看了 File.SetLastWriteTime,但 SharePoint 似乎不喜欢我触摸东西。

我也曾尝试使用 SharePoint 调用设置特征和上传文件,但由于我要发布到外部 SharePoint 实例,所以我无法进行身份验证,除非我使用 WebRequest 路由。

最佳答案

The uploading/syncing/etc functionality behaves fine but I am having issues with file properties. When uploading (code sample below) the files LastModified property is set to the time the file was uploaded. This is not the case if I directly Copy/Paste the file to the directory.

这是有道理的。试试这个,打开一个新的 Windows 资源管理器实例,选择一个文件,复制它,粘贴它。新文件的创建和访问日期将是今天的日期和时间,但修改日期将与原始日期一致。

上传文件时,您正在创建一个没有元数据的全新副本,因此修改日期和创建日期将与今天的日期和时间相匹配。

一旦文件在服务器上并且您可以获得它的句柄,您可以做的是使用 File.SetLastWriteTime 手动设置属性。 (参见 SetLastWriteTime documentation)。

希望对您有所帮助。

编辑 1:您可以迭代 Sharepoint 上的文件集合并通过执行以下操作在新文件上设置属性:

var list = web.Lists[new Guid("...")];
var folderItem = list.RootFolder.SubFolders;

foreach (File f in files) {
    var lastModifiedBy = context.Web.EnsureUser(f.LastModifiedBy);
    var lastModified = f.LastModified;
    SPFile uploadedFile = folderItem.Files.Add(f.FileName, f.Content, lastModifiedBy,
                          lastModifiedBy, lastModified, lastModified);
    uploadedFile.Item["Created"] = lastModified;
    uploadedFile.Item["Modified"] = lastModified;
    uploadedFile.Item.UpdateOverwriteVersion();
}

关于c# - 将文件流式传输到外部 SharePoint 时的上次修改属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19058369/

相关文章:

php - 传感器数据直播

c# - 使用 Ninject 过滤构造函数注入(inject)

c# - 解密加密值?

c# - Twitter API - OOB 流

web-services - REST API 404 : Bad URI, 或缺少资源?

java - 如何确定 Java 连接池是否与 URLConnection 一起工作?

javascript - SharePoint 和 jQuery 隐藏错误消息

sharepoint - .NET Sharepoint 创建目录

json - Sharepoint Online (2013) 列格式不起作用

C# NAudio : How to access the samples provided by SignalGenerator in order to save them to WAVE format?