9 Replies - 12490 Views - Last Post: 02 December 2010 - 07:03 PM Rate Topic: -----

#1 Sparukus  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 190
  • Joined: 05-April 10

C# upload file based on textbox path

Posted 02 December 2010 - 04:05 PM

Hey guys struggling to find a good tutorial on uploading a file.

Not quite sure how its done. Also Ive got this code so far, (using wpf) incase your wondering.

Basically it outputs the pathname to the textbox. (so I can save the path to the database) I was wondering how I could implement an upload into the code?

Something like textbox1.text pathname then save to some location the location is set so no need to browse where to saveas.

Code:

            // Create OpenFileDialog

            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();



            // Set filter for file extension and default file extension

            dlg.DefaultExt = ".txt";

            dlg.Filter = "";



            // Display OpenFileDialog by calling ShowDialog method

            Nullable<bool> result = dlg.ShowDialog();



            // Get the selected file name and display in a TextBox

            if (result == true)
            {

                // Open document

                string filename = dlg.FileName;

                textBox4.Text = filename;


Is This A Good Question/Topic? 0
  • +

Replies To: C# upload file based on textbox path

#2 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: C# upload file based on textbox path

Posted 02 December 2010 - 04:14 PM

I'm confused. You keep saying upload, but then you say WPF. Uploading is what you do to send a file to a webserver. And the end user never gets to "browse where to saveas" with an upload. You just get to pick a file to upload and let the web server do with it what it will.

It sounds like you're confusing two concepts.

This code doesn't upload anything. It chooses a file using an OpenFileDialog, then sets the path into a textbox.

So, I must ask, how do you intend on doing this upload? Do you already have a web page you plan on uploading to?

This post has been edited by insertAlias: 02 December 2010 - 04:15 PM

Was This Post Helpful? 1
  • +
  • -

#3 Sparukus  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 190
  • Joined: 05-April 10

Re: C# upload file based on textbox path

Posted 02 December 2010 - 04:28 PM

sorry, i never knew the server took care of the file once uploaded. I thought you chose the destination, atleast thats what I want to do, choose a path on the server "in my code" to send the "specified file" which is choosen from the user/admin via the openfiledialog broswe button.

Is that possible? Id also like to also have local aswell as uploading to a server. Maybe its just a save. Away to check on C#s save functions.

:)
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: C# upload file based on textbox path

Posted 02 December 2010 - 04:39 PM

Well, if this is going to be a WPF application, you need to think of how you're setting up the server side of this. You need two different applications, if it's WPF. One for the front end, and one on the server. Is it going to be ASP.NET? A web service? WCF? All these things are important, because you'd handle this differently for each.


As to choosing where to upload the file...everything's possible, but this is not going to be easy. If it's your application on the server, you could set it up to accept two inputs, a string and a file, and then use the string as the save as location, but there's no way for you to open a SaveFileDialog for the server on your local computer.

And as for a local version...why? The file is already on your computer, why do you want to save it again? If you need to do this, you can just move or copy the file. Look into System.IO if you want to do that programatically.
Was This Post Helpful? 1
  • +
  • -

#5 Sparukus  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 190
  • Joined: 05-April 10

Re: C# upload file based on textbox path

Posted 02 December 2010 - 04:52 PM

arghhh bandits!!! so theres no way to ftp a file or maybe use a network drive to upload a image file to a server, should have said its on the same network, my bad.

I dont want anything server side apart from my sql database but I dont want the picture stored in the database for certain reasons.
Was This Post Helpful? 0
  • +
  • -

#6 Sparukus  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 190
  • Joined: 05-April 10

Re: C# upload file based on textbox path

Posted 02 December 2010 - 04:59 PM

.

This post has been edited by Sparukus: 02 December 2010 - 05:28 PM

Was This Post Helpful? 0
  • +
  • -

#7 Sparukus  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 190
  • Joined: 05-April 10

Re: C# upload file based on textbox path

Posted 02 December 2010 - 05:28 PM

still keep getting the filepath error

            if (result == true)
            {

                string filename = dlg.FileName;
                string sourcePath = filename;
                string targetPath = @"C:\Users\blabla\Desktop\New folder\";

                // Use Path class to manipulate file and directory paths.
                string sourceFile = System.IO.Path.Combine(sourcePath, filename);
                string destFile = System.IO.Path.Combine(targetPath, filename);

                

                textBox4.Text = filename;
                File.Copy(filename, targetPath, true);


                }


Could not find a part of the path 'C:\Users\blabla\Desktop\New folder\'.

This post has been edited by Sparukus: 02 December 2010 - 05:31 PM

Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: C# upload file based on textbox path

Posted 02 December 2010 - 06:34 PM

There certainly are ways to FTP in C#

http://msdn.microsof...webrequest.aspx

Also, there are ways to move files to network paths or mapped drives, you can treat them the same as regular paths.

As for the error you're getting, does that folder exist? If not, you'll need to create it. I prefer to use System.IO.DirectoryInfo for that. Also, you'll need permissions to write to that path. I suggest trying a simple path first, like directly on the C: drive. I have a c:\dev folder that I use for all that stuff.
Was This Post Helpful? 0
  • +
  • -

#9 Sparukus  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 190
  • Joined: 05-April 10

Re: C# upload file based on textbox path

Posted 02 December 2010 - 06:49 PM

ooooo im lost now :( i made a mistake, Ive been saving the filepath of the source i wanted to save the filepath of the destination DEAR LORD!!!

basically i want to "copy" a file by opening a filedialog choosing a file to copy, and then setting that files destination path + filename(tocopy) to my textbox.

oooo ballls im so lost now :( shit
Was This Post Helpful? 0
  • +
  • -

#10 Sparukus  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 190
  • Joined: 05-April 10

Re: C# upload file based on textbox path

Posted 02 December 2010 - 07:03 PM

Yeah I have premissions same problem for c:\

keeps getting the error for part of the path now

updated code (solved my problem) but error remains:

            if (result == true)
            {

                string filename = dlg.SafeFileName;
                textBox4.Text = @"C:\Users\blabla\Desktop\New folder\" + filename;
                {

                    string sourcePath = @"C:\Users\blabla\Desktop\New folder\";
                    string destPath = System.IO.Path.Combine(sourcePath, filename);
                    File.Copy(sourcePath, destPath, true);


                }


wait woooohoooooooooooooo got it :D:D:D:D:D

            if (result == true)
            {

                string filename = dlg.SafeFileName;
                textBox4.Text = @"C:\Users\blabla\Desktop\New folder\" + filename;
                {

                    string sourcePath = @"C:\Users\blabla\Desktop\New folder\";
                    string destPath = System.IO.Path.Combine(sourcePath, filename);
                    File.Copy(dlg.FileName, destPath, true);


This post has been edited by Sparukus: 02 December 2010 - 07:03 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1