Search code examples
c#winforms

upload files with a C# windows forms project to a webserver


I finished an application in c# using winform to upload an image to web server. I searched this site and other sites to find an answer how to upload the file but no answer solved the problen..

I found code to upload file (image) but it gives me some errors then I found that I have to write a code in server side to procces the file ..

this is the code I used :

private async void BtnUpload_Click(object sender, EventArgs e)
{
    //c# open file dialog with image filters
    using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp; *.png)|*.jpg; *.jpeg; *.gif; *.bmp; *.png" })
    {
        // Get the file path from the use
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            //c# display image in picture box  
            pictureBox1.Image = new Bitmap(ofd.FileName);
            //c# show image file path  
            txtFileName.Text = ofd.FileName;
            //c# upload image to web server
            using (var fileStream = File.Open(ofd.FileName, FileMode.Open))
            {
                // Set the URL of the server script that handles the upload
                var client = new RestClient("https://blabla.com/Images/");
                var request = new RestRequest(Method.Post.ToString());
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    await fileStream.CopyToAsync(memoryStream);
                    request.AddFile("file", memoryStream.ToArray(), ofd.FileName);
                    request.AlwaysMultipartFormData = true;
                    var response = await client.ExecuteAsync(request);
                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        dynamic json = JsonConvert.DeserializeObject(response.Content);
                        string fileName = json.fileName;
                        MessageBox.Show("Success uploading image");
                    }
                    else
                    {
                        MessageBox.Show("Failed uploading image");
                    }
                }
            }
        }
    }
}

In this code I have this error :

System.IO.IOException: 'The process cannot access the file 'D:\NATRO\IMG-20240813-WA0002.jpg' because it is being used by another process.' and did not find solition for it..

then I use this code insted : using (var fileStream = File.OpenRead(ofd.FileName)) and the result is (as MessageBox)

Failed uploading image

Also I tried File.OpenWrie File.and no result.

In this article [https://stackoverflow.com/questions/7110300/uploading-images-in-winforms-application] I did not found a appropriate solution..

If I have to write a code in server side where exactly should I write it??

Hope to find any usefull answers.

Thanks and regards...


Solution

  • Are you sure you want to upload files directly to your http? Better use ftp because http files have read-only permissions. Or you want to use a script like php to validate your credentials and process the upload. Also your RestRequest is wrong. use: var request = new RestRequest(Method.Post);