How to attach files via ASP.net application to FogBugz with C#

491 Views Asked by At

I have an ASP.net application that allows the users to report bugs and attach files. The bug together with its detail and attachments should be saved in FogBugz. I have managed to create everything except the file attachment part.

here is my code:

private void NewCaseWithFile()

    {
        string fbUrl = "https://test.fogbugz.com/api.asp";
        string fbToken = logInFogBugz();
        string param = "";


        param += "cmd=new";
        param += "&token=" + fbToken;


        param += "&sTags=" + "OnlineService,";
        param += "&sTitle=" + "Testing";

        param += "&sEvent=" + "This case is being created from Visual Studio";
        param += "&nFileCount=" + "1";
        param += "&File1=" + "Picture.png";


        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(fbUrl + "?" + param);
        httpWebRequest.Method = WebRequestMethods.Http.Post;
        httpWebRequest.ContentType = "multipart/form-data";

        httpWebRequest.Accept = "application/xml";
        httpWebRequest.ContentLength = 0;

        HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse();
        StreamReader streamReader = new StreamReader(response.GetResponseStream());
        XDocument doc = XDocument.Load(streamReader);
    }

I have tried all instructions under "Editing Cases" but it did not help. In fact I have no idea what are File 1, File 2 and how to send them to FogBugz.

Can anyone help me with this? Many thanks!

1

There are 1 best solutions below

2
Michael Pryor On

File1 should be specified in the body of your multipart/form-data post (not as a querystring parameter).

You actually have to specify all the bytes in the file.

There's an answer on fogbugz.stackexchange.com as well as a C# FogBugz API wrapper that will handle all the parts for you.

The form parts in the body of your post would look like

--sdfjdsjsdflk SOME BOUNDARY--
Content-Disposition: form-data; name="File1"; filename="foo.jpg"
Content-Transfer-Encoding: base64
Content-Type: image/png

slfkajdflksjflajfdj
sldfjsd;aljfds
these are actual data bytes from the foo.jpg file
slfkjdsfljds
sdflajsdfs

Or you can look at this question which points to an RFC with an example.