Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7445501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:50:48+00:00 2026-05-29T11:50:48+00:00

Here is my code: private void UploadFilesToRemoteUrl(string url, string[] files, string logpath, NameValueCollection nvc)

  • 0

Here is my code:

private void UploadFilesToRemoteUrl(string url, string[] files, string logpath, NameValueCollection nvc)
        {
            long length = 0;
            string boundary = "----------------------------" +
            DateTime.Now.Ticks.ToString("x");

            HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest2.ContentType = "multipart/form-data; boundary=" + boundary;
            httpWebRequest2.Method = "POST";
            httpWebRequest2.KeepAlive = true;
            httpWebRequest2.Credentials = System.Net.CredentialCache.DefaultCredentials;

            Stream memStream = new System.IO.MemoryStream();

            byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");


            string formdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";

            foreach(string key in nvc.Keys)
            {
                string formitem = string.Format(formdataTemplate, key, nvc[key]);
                byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
                memStream.Write(formitembytes, 0, formitembytes.Length);
            }


            memStream.Write(boundarybytes,0,boundarybytes.Length);

            string headerTemplate = "Content-Disposition: form-data; name=\"{0}\";filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n";

            for(int i=0;i<files.Length;i++)
            {

                string header = string.Format(headerTemplate,"file"+i,files[i]);

                byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);

                memStream.Write(headerbytes,0,headerbytes.Length);

                FileStream fileStream = new FileStream(files[i], FileMode.Open,
                FileAccess.Read);
                byte[] buffer = new byte[1024];

                int bytesRead = 0;

                while ( (bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0 )
                {
                    memStream.Write(buffer, 0, bytesRead);
                }

                memStream.Write(boundarybytes,0,boundarybytes.Length);
                fileStream.Close();
            }

            httpWebRequest2.ContentLength = memStream.Length;

            Stream requestStream = httpWebRequest2.GetRequestStream();

            memStream.Position = 0;
            byte[] tempBuffer = new byte[memStream.Length];
            memStream.Read(tempBuffer,0,tempBuffer.Length);
            memStream.Close();
            requestStream.Write(tempBuffer,0,tempBuffer.Length );
            requestStream.Close();


            WebResponse webResponse2 = httpWebRequest2.GetResponse();
            Stream stream2 = webResponse2.GetResponseStream();
            StreamReader reader2 = new StreamReader(stream2);

            MessageBox.Show(reader2.ReadToEnd());

            webResponse2.Close();
            httpWebRequest2 = null;
            webResponse2 = null;

        }

And here is how I call it:

string[] filenames = new string[] { @"C:\Users\John\Desktop\ex.txt" };
NameValueCollection nvc = new NameValueCollection();
nvc.Add("cmd", "new");
nvc.Add("sTitle", "bugX");
nvc.Add("token", "someToken");
UploadFilesToRemoteUrl("https://myUrl.fogbugz.com/api.asp", filenames, "", nvc);

And everything is working fine, except the file isn’t uploaded.

I also tried this code:

http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data

and the same thing happens. How to resolve this?

Here is the response from the server:

<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<response>
    <case ixBug=\"123486\" operations=\"edit,assign,resolve,email,remind\"></case>
</response>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:50:51+00:00Added an answer on May 29, 2026 at 11:50 am

    you’re just sending a stream to the server, it’s impossible to know what happens at the server for us if you don’t provide any code for us from the server side. I guess you’ve forgotten to receive and create the file on the server side.

    if this is not the problem I would suggest you get the invaluable tool fiddler and start catching your httprequests and see what they contain.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Got this line of code here but its not working. private void Button_Click(object sender,
here is my example code: Public Class Parent Private _TestProperty As String Private WithEvents
Here is a VB.NET code snippet Public Class OOPDemo Private _strtString as String Public
I have some code here: private void button1_Click(object sender, EventArgs e) { Application.Run(new Form3());
Here's my code: private void Form1_Load(object sender, EventArgs e) { maskedTextBox1.Mask = *[L]; maskedTextBox1.MaskInputRejected
I have some code here private void Run() { MyClass c = new MyClass();
Here's my code: private void OnReceive(IAsyncResult result) { NetStateObject state = (NetStateObject)result.AsyncState; Socket client
Here's my code: private void LoadStudents() { Int64 gradeInstanceId = Convert.ToInt64(cmbGradeInstance.SelectedValue); StudentInstanceRepository studentInstanceRepo =
Here's my code: private void makeDialog2() { AlertDialog.Builder about = new AlertDialog.Builder(getContext()); about.setTitle(You Won!);
Here's the code: private void sendFile(InputStream file, OutputStream out) throws IOException { Log.d(TAG, trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.