-3

I wanted to upload file using C# for which I need its Name, Type and size etc as an example given below. Please let me know, how can I do that.

[file2] => Array
        (
            [name] => MyFile.jpg
            [type] => image/jpeg
            [tmp_name] => /tmp/php/php6hst32
            [error] => UPLOAD_ERR_OK
            [size] => 98174
        )
)

Here is my existing code:

private void choosFileBtn_Click(object sender, EventArgs e) {
    OpenFileDialog ofdChoos = new OpenFileDialog(); 

    if (ofdChoos.ShowDialog() == DialogResult.OK) 
    { 
        System.Net.WebClient Client = new System.Net.WebClient(); 
        // Client.Headers.Add("Content-Type", "binary/octet-stream"); 

        var sourceString = ofdChoos.FileName.Replace(@"\", @"/"); var source = @"" + sourceString;
        var result = Client.UploadFile("https://zaffarology.com/upload.php", "POST", @source); 

        string s = System.Text.Encoding.UTF8.GetString(result, 0, result.Length); 
    } 
}
Cory
  • 1,794
  • 12
  • 21
J Smith
  • 27
  • 6
  • That code doesn't look like C#.. – stuartd Oct 11 '18 at 12:55
  • What have you tried? where did you get stuck? what did you google? – BugFinder Oct 11 '18 at 12:55
  • That is PHP. Are you trying to say you want to upload to a php script from C#? – Cory Oct 11 '18 at 12:56
  • @Cory, Yea this is PHP, I need same thing in C# – J Smith Oct 11 '18 at 12:57
  • Well, that bit accepts a file, so you want to accept files via C# or upload the file via C#? You still aren't clear. If you can post the full PHP then that would help. – Cory Oct 11 '18 at 12:58
  • [FileInfo](https://learn.microsoft.com/en-us/dotnet/api/system.io.fileinfo?view=netframework-4.7.2) would seem like a good place to start your research... – Diado Oct 11 '18 at 12:58
  • @BugFinder, Actually I need answer of this [Question](https://stackoverflow.com/questions/52539590/how-to-upload-some-files-of-any-type-to-online-server-by-sending-data-to-php-api) but no one was replying – J Smith Oct 11 '18 at 13:00
  • @Cory, I wanted to upload file to online server using PHP API which requires file detail like file's type, size, name etc in the form of array in C#. That's why I posted PHP example – J Smith Oct 11 '18 at 13:03
  • 1
    The question you mentioned that no-one was replying to has two duplicates marked ([1](https://stackoverflow.com/questions/16416601/c-sharp-httpclient-4-5-multipart-form-data-upload), [2](https://stackoverflow.com/questions/42212406/how-to-send-a-file-and-form-data-with-httpclient-in-c-sharp)), both of which have accepted answers which look useful at the very least. – Diado Oct 11 '18 at 13:05
  • @Diado I had tried these but did not solve my problem – J Smith Oct 11 '18 at 13:07
  • @Cory Can't help me out? – J Smith Oct 11 '18 at 13:29
  • @JSmith there are bits of info, but it just doesnt seem like you've done anything yourself. The info you require has been offered to you but not in a "type this" format. – BugFinder Oct 11 '18 at 13:35
  • @JSmith I can, I’m away from the computer. I’ll check back in a bit – Cory Oct 11 '18 at 13:44
  • @Cory Thank You, I have solved the issue but facing another. I have got the required array by the following code(I'll share in next comment) – J Smith Oct 11 '18 at 16:29
  • `private void choosFileBtn_Click(object sender, EventArgs e) {OpenFileDialog ofdChoos = new OpenFileDialog(); if (ofdChoos.ShowDialog() == DialogResult.OK) { System.Net.WebClient Client = new System.Net.WebClient(); // Client.Headers.Add("Content-Type", "binary/octet-stream"); var sourceString = ofdChoos.FileName.Replace(@"\", @"/"); var source = @"" + sourceString;` – J Smith Oct 11 '18 at 16:31
  • `var result = Client.UploadFile("https://zaffarology.com/upload.php", "POST", @source); string s = System.Text.Encoding.UTF8.GetString(result, 0, result.Length); } }` – J Smith Oct 11 '18 at 16:32
  • @Cory Sorry for pacing code in two comments due to characters limit. The problem is Type is not coming correct as in [file2] => Array ( [name] => MyFile.jpg [type] => image/jpeg [tmp_name] => /tmp/php/php6hst32 [error] => UPLOAD_ERR_OK [size] => 98174 ) ) – J Smith Oct 11 '18 at 16:50
  • @JSmith, you can edit your post. I've edited it, waiting for a peer review then it will be active. I'll take a look now. – Cory Oct 11 '18 at 17:37
  • @JSmith do you have access to the PHP file? Can you post it? Are you able to modify it to make it easier for you? – Cory Oct 11 '18 at 17:43
  • @Cory, No I don't have access to PHP file, I can access it only by upload.php – J Smith Oct 12 '18 at 09:24
  • @Cory I got every thing in array correctly but my file type is not coming correctly – J Smith Oct 12 '18 at 09:26
  • @Cory **Problem Solved** **Thanks to all** – J Smith Oct 12 '18 at 10:55

1 Answers1

0

I have solved problem myself. Thanks to all who replied me and tried their best to help. Really Appreciated!

Solution Code

OpenFileDialog ofdChoos = new OpenFileDialog();
                if (ofdChoos.ShowDialog() == DialogResult.OK)
                {
                    System.Net.WebClient Client = new System.Net.WebClient();
                    var sourceString = ofdChoos.FileName.Replace(@"\", @"/");
                    var source = @"" + sourceString;

                    string exts = Path.GetExtension(source);
                    string ext  = exts.Replace(@".", string.Empty);
                    Client.Headers.Add("Content-Type", ext);

                    var result = Client.UploadFile("https://zff.com/upload.php", "POST", @source);
                    string s = System.Text.Encoding.UTF8.GetString(result, 0, result.Length);
                    MessageBox.Show(s);
                }
J Smith
  • 27
  • 6