private void btnSelectFile_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Multiselect = false; ofd.CheckFileExists = true; ofd.AutoUpgradeEnabled = true; ofd.Title = "请选择要上传的模板文件"; ofd.Filter = "Word 文档(*.doc,*.docx)|*.doc;*.docx"; DialogResult dr = ofd.ShowDialog(this); if (dr == DialogResult.OK) { //文件选择完毕 try { Stream stream = ofd.OpenFile(); if (stream.Length / 1024 / 1024 > 10) { stream.Close(); DXMessageBox.ShowWarning("文件体积过大,请不要超过10MB!"); return; } byte[] bytes = new byte[stream.Length]; int i = stream.Read(bytes, 0, bytes.Length); if (i > 0) { byte[] comBytes = StringPlus.CompressBytes(bytes); _fileBase64 = Convert.ToBase64String(comBytes); _fileBase64Ext = Path.GetExtension(ofd.FileName); txtFilePath.Text = ofd.FileName; } else { DXMessageBox.ShowWarning("读取文件失败!"); } } catch (Exception ex) { DXMessageBox.ShowWarning("文件打开失败! " + ex.Message); } } }