Click or drag to resize

MessageControllerGetAttachment Method

Returns the requested message attachment based on the the logged in User's account.

Namespace:  MedTunnelMsg.Controllers
Assembly:  MedTunnelMsg (in MedTunnelMsg.dll) Version: 1.0.0.0 (1.0.0.0)
Syntax
public HttpResponseMessage GetAttachment(
	int MessageId,
	int AttachmentId,
	bool SetReadFlag = true
)

Parameters

MessageId
Type: SystemInt32
The Id of the Message.
AttachmentId
Type: SystemInt32
The Id of the Attachment to retrieve.
SetReadFlag (Optional)
Type: SystemBoolean
Mark the attachment as viewed? Default is true.

Return Value

Type: HttpResponseMessage
HttpResponseMessage
  • 200 = Success
  • 401 = Unauthorized
  • 404 = Not Found
  • 500 = Internal Server Error
Examples
C#
    int messageId = 999999;
    Stream messageAttachment;
    string authKey = "";
    WebRequest request;
    HttpWebResponse response;
    Stream dataStream = null;
    string url = "";
    int attachmentId = 0;
    string attachmentFileName = "";

    try
    {
        List<Attachment> attachmentList = GetMessageAttachments(messageId);
        attachmentId = attachmentList[0].Id;
        attachmentFileName = attachmentList[0].DisplayName;

        string fileExtension = attachmentFileName.Substring(attachmentFileName.LastIndexOf('.') + 1);
        string fileName = attachmentFileName.Substring(0, attachmentFileName.LastIndexOf('.'));

        url = "https://server.medtunnel.com/MedTunnelMsg/api/Message/GetAttachment?messageid=" + messageId + "&attachmentid=" + attachmentId + "&setreadflag=false";

        request = WebRequest.Create(url);
        request.Headers["Authorization"] = authKey;
        request.Headers[HttpRequestHeader.ContentType] = "application/json";

        response = (HttpWebResponse)request.GetResponse();
        dataStream = response.GetResponseStream();

        if (response.StatusCode != HttpStatusCode.OK || dataStream == null)
        {
            throw new Exception("Failed: " + response.StatusCode.ToString());
        }
    }
    catch (Exception ex)
    {
        throw new Exception("Failed: " + ex.Message);
    }

    //...your code to handle the returned attachment stream

}

private List<Attachment> GetMessageAttachments(int MessageId)
{
    List<Attachment> attachmentList = new List<Attachment>();

    if (MessageId > 0)
    {
        WebClient client = new WebClient();
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        MedTunnelMsg.Models.Result webClientResult;

        client.Headers["Authorization"] = authKey;
        client.Headers[HttpRequestHeader.ContentType] = "application/json";
        url = "https://server.medtunnel.com/MedTunnelMsg/api/Message/GetAttachments?messageid=" + MessageId;

        String attachments = client.DownloadString(url);

        webClientResult = serializer.Deserialize<Result>(attachments);
        attachmentList = serializer.Deserialize<List<Attachment>>(webClientResult.Data.ToString());
    }

    return attachmentList;
}
See Also