uploading picture - c#

Good day
Creating an Inventory system
created a db in sql, In visual c#, I created a form where products will be entered and allowing the user to upload an image of the item, How do I go about doing it?
I'm a c++ programmer, newly into visual c#
Thanks

Here is an older post on uploading on uploading a file.

Assuming you'd want to save the images in the database as well... You need to allow the user to select an image from his/her own harddisk (Open File Dialog), and then read the bytes and send them to the database (ADO.NET's DbCommand). ADO.NET supports streams for BLOBS.
Here is a example of the Open File Dialog:
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "images (*.png)|*.png|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
using (Stream myStream = openFileDialog1.OpenFile())
{
if (myStream != null)
{
// do something with the stream bytes here....
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}

Related

Get complete file path from file click in File Explorer

Using Visual Studio 2017 and Windows 10 I want to be able to open a file explorer and navigate to a file outside of the program. Once my file is collected I want to get the file path and the complete file name for the file explorer.
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "All files (*.*)|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
textBoxFolderpath.Text= openFileDialog1.FileName;
if (textBoxFolderpath.Text.Contains('\\'))
textBoxFolderpath.Text = textBoxFolderpath.Text.Substring(0, textBoxFolderpath.Text.LastIndexOf('\\'));
This is what I have tried so far. The textBoxFolderpath is being used to hold the values.
What am I missing or doing incorrectly?
When working with filenames and paths I'd highly recommend using built-in classes to handle this, like the Path class:
Path.GetDirectoryName(openFileDialog1.FileName)
This returns the path, without the filename. It's also cross-platform compatible.
Thanks Zer0 - below is what I ended up using.
private void btnCurrentFolder_Click(object sender, EventArgs e)
{
OpenFileDialog openDialog1 = new OpenFileDialog();
// Determine starting directory
if (chkSetToRoot.Checked)
{
openDialog1.InitialDirectory = #"K:\RESULTS";
}
openDialog1.Title = "Select A Test File";
openDialog1.Filter = "All Files (*.*)|*.*";
if (openDialog1.ShowDialog() == DialogResult.OK)
{
textBoxFolderpath.Text = Path.GetDirectoryName(openDialog1.FileName);
textBoxFileName.Text = Path.GetFileName(openDialog1.FileName);
}
}

How to open or download PDF file in Google Drive inside my application

Hi everyone I am trying to create an application (C# Windows Form Application) that can open and/or download PDF files from Google Drive (My drive)
This is a sample way to open a PDF file from my hard disc using Adobe PDF Reader (AxAcroPDF):
private void btn_OpenPDF_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "PDF Files (*.pdf)|*.pdf";
ofd.Title = "Open an PDF file";
if (ofd.ShowDialog() == DialogResult.OK)
{
apr_OpenPDF.src = ofd.FileName;
}
}
But what about reading a PDF files from Google Drive ?
Thanks for any help and sorry for my English
You can open, download and convert files using Google Drive API. When a user selects an app from the Open with contextual menu for a Google Doc, Drive redirects the user to the Open URL for the selected application. The redirected request from the user's browser contains this important information in the state parameter:
The action, set to open.
The exportIds to use.
Download and convert file content with the files.export method.
var fileId = "1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo";
var request = driveService.Files.Export(fileId, "application/pdf");
var stream = new System.IO.MemoryStream();
// Add a handler which will be notified on progress changes.
// It will notify on each chunk download and when the
// download is completed or failed.
request.MediaDownloader.ProgressChanged +=
(IDownloadProgress progress) =>
{
switch(progress.Status)
{
case DownloadStatus.Downloading:
{
Console.WriteLine(progress.BytesDownloaded);
break;
}
case DownloadStatus.Completed:
{
Console.WriteLine("Download complete.");
break;
}
case DownloadStatus.Failed:
{
Console.WriteLine("Download failed.");
break;
}
}
};
request.Download(stream);

Save DIalogue that copies/loads another file

I am trying to save a file, but the actual file is already built and saved in a temp location, and I just want to move/copy that pre-built file to wherever the user chooses with the save dialogue.
What I have right now is this
fileName = the pathway of the file that is already built.
private void SaveFile()
{
SaveFileDialog savefile = new SaveFileDialog();
savefile.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
if (savefile.ShowDialog() == DialogResult.OK)
{
using (StreamWriter sw = new StreamWriter(savefile.FileName))
sw.WriteLine(fileName);
}
}
Obviously right now this just writes the pathway to a text file, but I am trying to find a way to basically copy that file to wherever this user specifies.
you can do like
if (savefile.ShowDialog() == DialogResult.OK)
{
// you can use File.Copy
System.IO.File.Copy(fileName, saveFile.Filename);
}

How to disable OpenFileDialog opening files automatically?

I am using following code to select a file to import in a Windows Forms project.
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "C# Corner Open File Dialog";
fdlg.InitialDirectory = #"c:\";
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fdlg.FilterIndex = 2;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
txtpath.Text = fdlg.FileName;
}
The problem is that the selected file is opened in the background which I don't want. What can I do to just get the path of selected file without opening it?
Showing an OpenFileDialog and the user selecting a file does not open the file. The file can be opened by calling OpenFile. In the code you posted the file is not opened. That code appears to be copied from an example on MSDN. The rest of the code from that example is here:
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null) // File is opened here.
{
using (myStream)
{
// Insert code to read the stream here.
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " +
ex.Message);
}
}
If the file is being opened when you don't want it opened then the problem must be somewhere else and not in the code you posted. It is possible for example that you have not closed the file (for example by using Dispose) after you finished using it last time.

File Open/Save Dialog

I am using my own Custom View to show the files and folders and also using a search box to jump to a specific folder. In that case How to send a message to File Open/Save dialog to enforce it to change the current displayed folder.
e.g. If the dialog shows files and folders of current displaying folder "C:\", I want an API (or any piece of code) to enforce to change the current folder to "D:\"
You can have the dialog open at a specific directory using InitialDirectory.
If you want to control what the dialog does at runtime, that's a bit more complex.
Set SaveFileDialog.InitialDirectory after you create it, but before you open it.
For example:
Stream myStream = null;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1 .InitialDirectory = "d:\\" ;
saveFileDialog1 .Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
saveFileDialog1 .FilterIndex = 2 ;
saveFileDialog1 .RestoreDirectory = true ;
if(saveFileDialog1 .ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = saveFileDialog1 .OpenFile()) != null)
{
// Code to write the stream goes here.
myStream.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not save file to disk. Original error: " + ex.Message);
}
}
set InitialDirectory property to any path

Categories