Page 1 of 1

Add a New Item with Attachment

Posted: Wed Feb 06, 2008 6:09 am
by cs
Hi,

I am trying to add a new item into fortress with an attachment. I can add a new item fine but when I try to add an attatchment I get the following error:

[System.InvalidOperationException] = {"There was an error generating the XML document."}
InnerException = {"System.Web.HttpInputStream cannot be serialized because it does not have a parameterless constructor."}
Message = "There was an error generating the XML document."

I am using the following code to upload the attachment:

Code: Select all

        MantisItemAttachmentFullDetail objAttachment = new MantisItemAttachmentFullDetail();
        MantisItemAttachmentFullDetailColl objAttFullDetailCol = new MantisItemAttachmentFullDetailColl();

        HttpPostedFile hpfAttachment = filFile.PostedFile;
        if (hpfAttachment.ContentLength != 0)
        {
            string sStrippedPath = Common.StripPath(hpfAttachment.FileName);

            objAttachment.AttStream = hpfAttachment.InputStream;
            objAttachment.ContentType = hpfAttachment.ContentType;
            objAttachment.FileCRC = 0;
            objAttachment.FileLength = hpfAttachment.ContentLength;
            objAttachment.Description = "Test";
            objAttachment.FileName = sStrippedPath;
            objAttFullDetailCol.Add(objAttachment);
        }
I then use the following to add the attachment to the new item:

Code: Select all

        MantisItem objItem = new MantisItem();
        // Other item details here
        objItem.AddedAttachments = objAttachment;
        ItemTrackingOperations.ProcessCommandAddFortressItem(objItem);
Thanks for any assistance

Posted: Wed Feb 06, 2008 9:48 am
by shannon
You can't send a stream over the web service. The easiest way to do this would be to get the file to disk, then set the LocalPath, Description, and FileName attributes on the attachment object. Then you can add the attachments collection to the item and add the item the same way you were in the second code snippet.

Posted: Wed Feb 06, 2008 10:57 am
by cs
Thanks for your reply.

So this will mean I have to read the file in, write it to a file on the server, then pass the new file details into Fortress to read again? Is there no other way?

I have been looking into the MantisItemAttachmentFullDetail object and it gives me the following overloads:

Code: Select all

public MantisItemAttachmentFullDetail();

public MantisItemAttachmentFullDetail(int nMsgID, string strFileName, string strFullLocalPath, string strDescription);

public MantisItemAttachmentFullDetail(int nMsgID, int nAttID, string strFileName, string strDescription, int nFileCRC, int nFileLength, string strContentType, Stream s);
Can I use the third overload to send the clients file to the server?

Thanks

Posted: Wed Feb 06, 2008 12:02 pm
by mskrobul
You can't just send a file stream over a web service. The API uploads a file via an http request made outside the web service to get around this limitation.

The class you are referring to isn't intended for use over the web service. It is used for our web based client which doesn't send information over SOAP.

On your side, the only MantisItemAttachment parameters you need to set to upload an attachment are the local disk path and optionally the description.

Posted: Thu Feb 07, 2008 9:14 am
by cs
Thanks for the feedback.

So this means I have to:

1. Read the file into memory from the client pc
2. Write it to a new file on the server
3. Pass the new file details into Fortress to read again
4. Delete the newly created file off the server

Is this how you would do a file attachment?

Thanks

Posted: Thu Feb 07, 2008 9:46 am
by jclausius
I think there might be some confusion here.

Where is your custom application deployed? On the client's PC?

Posted: Thu Feb 07, 2008 9:47 am
by mskrobul
All you need to do is:

1. Set the LocalPath of the attachment to the client disk path to the file on the client.
2. Add the item via the API.

This is a rough example (not been compiled or anything):

Code: Select all

MantisItem item = new MantisItem();
MantisItemAttachmentFullDetail objAtt = new MantisItemAttachmentFullDetail();
MantisItemAttachmentFullDetailColl objAttFullDetailCol = new MantisItemAttachmentFullDetailColl(); 

objAtt.LocalPath = <client local disk path>;
objAtt.Name = filename;
objAtt.Description = <optional description>;

objAttFullDetailCol.Add(objAtt);
item.AddedAttachments = objAttFullDetailCol;

ItemTrackingOperations.ProcessCommandAddFortressItem(objItem); 
ProcessCommandAddFortressItem() takes care of uploading the attachment to the server for you.

Posted: Thu Feb 07, 2008 11:13 am
by cs
Thanks for getting back to me.

Our custom application is deployed on a server, using a front end web site for adding Fortress items.

Your code example works fine running in the IDE, but when I copy it over to the server and run it I get the error “Attachment upload failed". All other information for the new item is added correctly.

I have also tested that I can add attachments through the Fortress web client.

Do you know what causes this error?

Thanks

Posted: Thu Feb 07, 2008 12:32 pm
by mskrobul
I am not sure exactly what you mean by "Your code example works fine running in the IDE, but when I copy it over to the server...".

Are you saying that attachments work when you are on a machine that isn't the server machine, but fail when run on the server machine? Or does in run fine in Debug mode but not on the real server?

Or are you saying the code compiles fine but adding the attachment fails?

What version of IIS is the server running?

Posted: Fri Feb 08, 2008 3:36 am
by cs
The code runs fine on my development pc (not the server) running in debug mode. When I copy the code onto the server the file attachments no longer work giving me the “Attachment upload failed" error.

I am running IIS 6 on the server.

Thanks

Posted: Fri Feb 08, 2008 8:55 am
by mskrobul
Look at the following KB article for Vault. It also applies to attachment uploads because the method the API uses for attachments is essentially the same as what Vault uses to upload files.

http://support.sourcegear.com/viewtopic.php?t=735

Are there any more descriptive error messages logged to the sgfortress log file (%windir%\temp\sgfortres\fortress.log)?

Posted: Mon Feb 11, 2008 10:48 am
by cs
Thanks for all your help.

I spoke to one of your colleagues on Friday, he has said that I must do the following to get the attachments to work correctly:

1. Read the file into memory from the client pc
2. Write it to a new file on the server
3. Pass the new file details into Fortress to read again
4. Delete the newly created file off the server

This is very inefficient, but as this solution works I will leave it for the time being.

Thanks