Silverlight 4中的拖放文件上传?

3

我正在将一个WinForms应用程序重写为Silverlight。在WinForms应用程序中,有一个用例允许用户将TIFF图像(传真)从Outlook直接拖放到“将图像或传真附加到此案例”控件上。是否有一个Silverlight 4控件可以实现相同的功能?重要的是要意识到,将文件保存到本地文件系统,然后使用标准上传控件选择并上传文件将无法满足项目中概述的业务要求。

3个回答

2

Silverlight 4支持从文件系统将文件拖放到任何 UIElement。请参见此博客

但是,我不知道这是否适用于从Outlook启动的拖动操作。建议您从此博客获取示例并构建一个小型测试应用程序,以查看是否可以拖动附件。

当然,您仍需要对TIFF进行解码,以使Silverlight可以使用。


1
(我猜Silverlight由于其“轻量级”性质,许多功能将很难移植。您可能希望考虑使用Click-once WPF应用程序而不是Silverlight,特别是因为您已经依赖于一个丰富的安装在Windows上的应用程序)。
(您需要在Silverlight用户控件的构造函数中执行类似于这样的操作):
this.Drop += new DragEventHandler(MainPage_Drop);

然后添加一个方法

void MainPage_Drop(object sender, DragEventArgs e)
{
    IDataObject drop = e.Data;

    if (drop.GetDataPresent(System.Windows.DataFormats.FileDrop))
    {
        FileInfo[] files = (FileInfo[])e.Data.GetData(System.Windows.DataFormats.FileDrop);
        foreach (FileInfo file in files)
        {
          // do something with each file here
        }
    }
}

然后--看看会发生什么。您需要使用一个库来添加TIFF支持,可能是像Stackoverflow上建议的这里那样的东西。


0

在 Silverlight 应用程序中,您可以从桌面拖放文件。我已经在我们的项目中实现了这个功能。在 Silverlight 项目属性中勾选“需要提升的权限”,并使用 Silverlight 数据网格的拖放事件,就可以处理来自桌面的拖放操作。

    private void DocumentsDrop(object sender, DragEventArgs e)
    {
        e.Handled = true;

        var point = e.GetPosition(null);
        var dataGridRow = ExtractDataGridRow(point);
        if(dataGridRow !=null)
        {.....
         }

        var droppedItems = e.Data.GetData(DataFormats.FileDrop) as      FileInfo[];
        if (droppedItems != null)
             {
                var droppedDocumentsList = new List<FileInfo>();

                foreach (var droppedItem in droppedItems)
                {
                    if ((droppedItem.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
                    {
                        var directory = new DirectoryInfo(droppedItem.FullName);
                        droppedDocumentsList.AddRange(directory.EnumerateFiles("*", SearchOption.AllDirectories));
                    }
                    else
                    {
                        droppedDocumentsList.Add(droppedItem);
                    }
                }

                if (droppedDocumentsList.Any())
                {
                    ProcessFiles(droppedDocumentsList);
                }
                else
                {
                    DisplayErrorMessage("The selected folder is empty.");
                }
            }
   }

在 XAML 中设置 AllowDrop = true; 以启用 DataGrid 的拖放功能。从 DragEventArgs 中提取信息作为 FileInfo 对象。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接