Esempio n. 1
0
        public HttpResponseMessage GetAttachment([FromBody] AttachmentParams parameters)
        {
            using (SqlConnection connection = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DBCS"].ConnectionString))
            {
                SqlDataAdapter da = new SqlDataAdapter("SELECT DATA FROM ServiceAttachments WHERE USED_SERVICE_ID=@ServiceId AND NAME = @Name", connection);

                da.SelectCommand.Parameters.AddWithValue("@Serviceid", parameters.ServiceId);
                da.SelectCommand.Parameters.AddWithValue("@Name", parameters.Name);

                DataSet set = new DataSet();

                try
                {
                    da.Fill(set);

                    byte[] toPass = (byte[])set.Tables[0].Rows[0]["DATA"];

                    return(Request.CreateResponse(HttpStatusCode.OK, toPass));
                }
                catch (Exception ex)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message));
                }
            }
        }
        public ActionResult DownloadAttachment(string attachment_name, int service_id)
        {
            AttachmentParams parameters = new AttachmentParams()
            {
                Name      = attachment_name,
                ServiceId = service_id
            };

            MemoryCache cache = MemoryCache.Default;

            using (HttpClient client = WebApiClient.InitializeAuthorizedClient(Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/') + "/", "Bearer", cache["access_token"] as string))
            {
                HttpResponseMessage response = client.PostAsJsonAsync("/api/ServiceApi/GetAttachment", parameters).Result;

                if (response.IsSuccessStatusCode)
                {
                    byte[] data = response.Content.ReadAsAsync <byte[]>().Result;


                    FileContentResult result = new FileContentResult(data, "application/octet-stream")
                    {
                        FileDownloadName = attachment_name
                    };

                    return(result);
                }
            }

            return(new FileContentResult(null, ""));
        }