I have a small Inventory Control winforms project that I'm developing and need help with one aspect. My workflow is this:
1) User selects a SKU Number in a text box (SKUText), and the form is populated with all fields for that record.
2) User uploads an image by selecting a local image to be displayed in a picturebox. The image is uploaded to an FTP server and will be displayed from that path in the picturebox.
What I need help with is that I need to rename the file before the FTP upload so that it matches the SKU number without changing the extension. Im using [ftploc] as the full path and filename to pass to the FTP upload destination to as shown in my code.
Example: User uploads 'toy_truck.jpg' and I need the filename to become (SKUText.Text).jpg
Thanks the help in advance!
private void UploadButton_Click(object sender, EventArgs e)
OpenFileDialog fd = new OpenFileDialog();
fd.Filter = "Image Files(*.jpg; *.png; *.gif; *.bmp)|*.jpg; *.png; *.gif; *.bmp";
fd.Title = "Select File for Upload...";
if (fd.ShowDialog() == DialogResult.OK)
//Need fd.SafeFileName below renamed to text value of SKUText.Text, a textbox further up
//in my code, without affecting the file extension
String ftploc = ("ftp://x.x.x.x/" + fd.SafeFileName);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftploc);
request.Method = WebRequestMethods.Ftp.UploadFile;
// FTP Login
request.Credentials = new NetworkCredential("domain\\user", "pass");
request.Timeout = -1;
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = true;
request.Proxy = null;
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(fd.FileName);
byte fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
//ImageBox1.ImageLocation = ftploc;