打开文件对话框中CSHTML [英] OpenFileDialog in cshtml

查看:250
本文介绍了打开文件对话框中CSHTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可我知道我热可以写的东西像剃刀此C#Opendialog?我试图让一个OpenFileDialog,将提供用户上传照片到SqlServerCe数据库:

 打开文件对话框openFileDialog1 =新的OpenFileDialog();openFileDialog1.InitialDirectory =C:\\\\;
openFileDialog1.Filter =所有文件| *。*(*。*)。
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = TRUE;如果(openFileDialog1.ShowDialog()== DialogResult.OK)
{
   尝试
   {
      字符串路径= openFileDialog1.FileName;
      字节[] =图像File.ReadAllBytes(路径);
      查询字符串=UPDATE FIRMA SET标志= @图片WHERE ID = 1;
      的SqlCommand的SqlCommand =新的SqlCommand(查询,康涅狄格州);
      sqlCommand.Parameters.AddWithValue(@图像,图像);
      conn.Open();
      sqlCommand.ExecuteNonQuery();
      conn.Close();
   }
   赶上(异常前)
   {
      的MessageBox.show(错误:无法从磁盘中读取文件原来的错误:+ ex.Message);
   }


解决方案

您不能使用在Web应用程序的特定的OpenFileDialog(一拉C#/的WinForms / WPF)(不使用Silverlight或其他插件)。

所有你能做的就是用一个文件输入标签,这将导致浏览器当用户点击浏览按钮来打开它的默认文件浏览器对话框:

 <输入类型=文件名称=的ElementName/>

当的形式发布,该文件将作为输入流的一部分。

对于&LT的完整规范;输入> 元素,去这里:的 http://www.w3schools.com/tags/tag_input.asp

may I know hot can I write something like this c# Opendialog in Razor? I'm trying to make an openfiledialog that would offer user to upload photo into SqlServerCe database:

OpenFileDialog openFileDialog1 = new OpenFileDialog();

openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
   try
   {
      string path = openFileDialog1.FileName;
      byte[] image = File.ReadAllBytes(path);
      string query = "UPDATE firma SET logo=@Image WHERE id = 1";
      SqlCommand sqlCommand = new SqlCommand(query, conn);
      sqlCommand.Parameters.AddWithValue("@Image", image);
      conn.Open();
      sqlCommand.ExecuteNonQuery();
      conn.Close();
   }
   catch (Exception ex)
   {
      MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
   }

解决方案

You can't use a specific OpenFileDialog (a la c#/winforms/wpf) in a web application (without using Silverlight or some other plugin).

All you can do is use a file input tag, which will cause the browser to open its default file browser dialog box when the user hits the browse button:

<input type="file" name="elementName" />

When the form is posted, that file will be included as part of the input stream.

For the full specification of the <input> element, go here: http://www.w3schools.com/tags/tag_input.asp

这篇关于打开文件对话框中CSHTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆