Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenFileDialog in cshtml

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);
   }
like image 942
HollowIT Avatar asked Nov 05 '13 21:11

HollowIT


People also ask

What does OpenFileDialog use for?

In this article Forms. OpenFileDialog component opens the Windows dialog box for browsing and selecting files. To open and read the selected files, you can use the OpenFileDialog. OpenFile method, or create an instance of the System.

What is OpenFileDialog VB?

The OpenFileDialog control prompts the user to open a file and allows the user to select a file to open. The user can check if the file exists and then open it. The OpenFileDialog control class inherits from the abstract class FileDialog.

How do I open a file using OpenFileDialog in VB net?

Step 1: Drag the OpenFileDialog control from the toolbox and drop it to the Windows form, as shown below. Step 2: Once the OpenFileDialog is added to the form, we can set various properties of the OpenFileDialog by clicking on the OpenFileDialog. Video Player is loading.


1 Answers

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

like image 53
dodexahedron Avatar answered Oct 26 '22 06:10

dodexahedron