zoukankan      html  css  js  c++  java
  • LS 存取文件

    partial void GetFile_Execute()
            {
                // Download file from selected record.
                var doc = this.DocumentsSet.SelectedItem;
                if (doc == null) return;

                Dispatchers.Main.BeginInvoke(() =>
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    string defaultFilter = "Images *.jpg, *.png, *.gif|*.jpg;*.png;*.gif|Documents *.doc, *.pdf|*.doc;*.pdf|All *.*|*.*";
                    if (!string.IsNullOrEmpty(doc.FileName))
                    {
                        string ext = Path.GetExtension(doc.FileName);
                        sfd.DefaultExt = ext;
                        defaultFilter = string.Format("*.{0}|*.{0}|", sfd.DefaultExt) + defaultFilter;
                    }
                    sfd.Filter = defaultFilter;

                    bool? selected = sfd.ShowDialog();
                    if (selected.HasValue && selected.Value)
                    {
                        string fileName = sfd.SafeFileName;
                        long len = 0;
                        using (var fs = sfd.OpenFile())
                        {
                            // Write all bytes in field to file..
                            fs.Write(doc.Blob, 0, doc.Blob.Length);
                            len = fs.Length;
                        }

                        this.ShowMessageBox("File Get: " + sfd.SafeFileName + "\nSize:" + len);

                        // Could open file if you knew the path, but SafeFileName does not give path.
                        // string path = @"c:\temp\" + fileName;
                        // var shell = AutomationFactory.CreateObject("Shell.Application");
                        // shell.ShellExecute("mspaint.exe", path, "", "open", 1);
                    }
                    else
                        this.ShowMessageBox("Cancelled");
                });
            }

            partial void EditFile_Execute()
            {
                // Upload file to selected records image field.
                var doc = this.DocumentsSet.SelectedItem;
                if (doc == null) return;

                Dispatchers.Main.BeginInvoke(() =>
                {
                    OpenFileDialog ofd = new OpenFileDialog();

                    bool? selected = ofd.ShowDialog();
                    if (selected.HasValue && selected.Value)
                    {
                        byte[] ba = null;
                        using (var fs = ofd.File.OpenRead())
                        {
                            ba = new byte[fs.Length];
                            int pos = 0;
                            while (pos < fs.Length)
                            {
                                int read = fs.Read(ba, pos, ba.Length - pos);
                                pos += read;
                            }
                            this.ShowMessageBox("File Put: " + fs.Name + "\nSize:" + fs.Length);
                        }

                        // Set document and doc name in fields. To be Saved by user.
                        doc.FileName = ofd.File.Name;
                        doc.Blob = ba;
                    }
                    else
                        this.ShowMessageBox("Cancelled");
                });
            }

  • 相关阅读:
    前端面试题
    Less 中的嵌套规则与 & 符号的使用
    组件的生命周期
    222
    221
    twemproxy代理memcache(3)
    memcache数据迁移与解决方案(2)
    mencache的原理与应用(1)
    yield推荐,理解协程的最佳入口
    上下文管理器的实现
  • 原文地址:https://www.cnblogs.com/ysharp/p/2140731.html
Copyright © 2011-2022 走看看