c# – cshtml中的OpenFileDialog
发布时间:2020-12-15 08:36:09 所属栏目:百科 来源:网络整理
导读:我可以知道我可以在Razor中写这样的c#Opendialog吗?我正在尝试制作一个openfiledialog,它可以让用户将照片上传到SqlServerCe数据库: OpenFileDialog openFileDialog1 = new OpenFileDialog();openFileDialog1.InitialDirectory = "c:";openFileDialog1.F
我可以知道我可以在Razor中写这样的c#Opendialog吗?我正在尝试制作一个openfiledialog,它可以让用户将照片上传到SqlServerCe数据库:
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); } 解决方法
您不能在Web应用程序中使用特定的OpenFileDialog(la c#/ winforms / wpf)(不使用Silverlight或其他插件).
您所能做的就是使用文件输入标记,这将导致浏览器在用户点击浏览按钮时打开其默认文件浏览器对话框: <input type="file" name="elementName" /> 发布表单时,该文件将作为输入流的一部分包含在内. 对于< input>的完整规范元素,请到这里:http://www.w3schools.com/tags/tag_input.asp (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |