Translate

Home > September 2021

September 2021

Angular Download File from server , C# Rest Api

Thursday, September 9, 2021 Category : , 0

 HTML

 

<div class="input-group-append">
       <button type="button" class="btn btn-primary"
           (click)="downloadButtonClick('inputGroupFile03')">
            <i class="fas fa-download"> </i>
             Download</button>
 </div>

 

  Type Script


  downloadButtonClick(fileNamestring) {


  
    let binaryDataany = [];

    this.http.get(this.env.apiUrl + 'api/Supplier/get_doc_details_by_id2?id=' + this.model.SupID , { responseType: "blob" }).subscribe(data => {

      binaryData.push(data);

      let dataType = data.type;
      let downloadLink = document.createElement('a');
      downloadLink.href = window.URL.createObjectURL(new Blob(binaryData, { type: dataType }));

        downloadLink.setAttribute('download', "File Name.ext");
   

      document.body.appendChild(downloadLink);
      downloadLink.click();



    }, error => {
      console.error(error)
      this.alertService.error(error);
    });




  }

Web Api
 
 
[Authorize(Policy = nameof(Policy.Account))]
[HttpGet("get_doc_details_by_id2")]
public ActionResult GetDocDetailsById2(string id)
{
try
{

var data = _shopService.FindDocumentById(id);
                 if(
data != null) {
                     string fileType_ = "application/octet-stream";
if (data.fileName.ToLower().Contains("pdf"))
{
fileType_ = "application/pdf";
}
return File(data.fileByteArray, fileType_ , data.filename);
                 }
 
                return NotFound();
}
catch (Exception ex)
{
return StatusCode(500, ex.Message + ex.StackTrace);
}

C# AWS SNS Subscribtion configuration

Wednesday, September 1, 2021 Category : 1

Install Nuget Dependency

 

 

public class Messages
    {
        public string Type { get; set; }
        public string MessageId { get; set; }
        public string Token { get; set; }
        public string TopicArn { get; set; }
        public object Message { get; set; }
        //public Message Message2 { get; set; }
        public string SubscribeURL { get; set; }
        public string Timestamp { get; set; }
        public string SignatureVersion { get; set; }
        public string Signature { get; set; }
        public string SigningCertURL { get; set; }
        public string Subject { get; set; }
        public string UnsubscribeURL { get; set; }
    }

 

     [Route("api/awssns")]
    [ApiController]
    public class ValuesController : ControllerBase
    {

        private readonly ILogger<ValuesController> _logger;

        public ValuesController(ILogger<ValuesController> logger)
        {
            _logger = logger;
        }

       

        [HttpPost("[action]")]
        public async Task<IActionResult> webhook()
        {

            var re = Request;
            var headers = re.Headers;
            string body = "";
            try
            {

                string messagetype = Request.Headers["x-amz-sns-message-type"];
                //if (headers.Contains("Custom"))
                //{
                //    string token = headers.TryGetValue("Custom").First();
                //}
                //Logger.Debug(messagetype);

                _logger.LogInformation("**** MSDSL BEGIN LOG ****");
                _logger.LogInformation(messagetype);


                //HttpContext.Request.Body.Seek(0, SeekOrigin.Begin);

                using (StreamReader stream = new StreamReader(HttpContext.Request.Body))
                {
                     body = stream.ReadToEnd();
                    // body = "param=somevalue&param2=someothervalue"
                    _logger.LogInformation(body);


                    

                    Messages msg = JsonConvert.DeserializeObject<Messages>(body);

                    if (msg != null)
                    {
                        if (msg.SignatureVersion.Equals("1"))
                        {
                            //temp
                            {
                                var aws = Amazon.SimpleNotificationService.Util.Message.ParseMessage(body);
                                bool r = aws.IsMessageSignatureValid();
                                if (r)
                                {
                                    _logger.LogInformation("Signature verification succeeded");
                                }
                                else
                                {
                                 Message message = JsonConvert.DeserializeObject<Message>(msg.Message.ToString());
                                    _logger.LogInformation("Signature verification failed");
                                    await new LogManager().Insert(new Client_Log { Message = JsonConvert.SerializeObject(msg.Message), Response = body, Error = "Signature verification failed", timestamp = DateTime.Now }, message.merchantWallet);
                                    return BadRequest("Signature verification failed");
                                }
                            }
                            if (messagetype.Equals("Notification"))
                            {
                                _logger.LogInformation("Subject : " + msg.Subject);


                                 Message message = JsonConvert.DeserializeObject<Message>(msg.Message.ToString());
                                

// do what you want

                                                           }
                            else if (messagetype.Equals("SubscriptionConfirmation"))
                            {
                                 

                                System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();

                                var response = await client.GetAsync(msg.SubscribeURL);
                                response.EnsureSuccessStatusCode();
                            }
                            else if (messagetype.Equals("UnsubscribeConfirmation"))
                            {
                            
                                System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();

                                var response = await client.GetAsync(msg.UnsubscribeURL);
                                response.EnsureSuccessStatusCode();
                            }
                        }
                        else
                        {
                                 Message message = JsonConvert.DeserializeObject<Message>(msg.Message.ToString());
                            _logger.LogInformation("Unexpected signature version. Unable to verify signature.");
                                                     

                        }
                    }
                    else
                    {
                                        await new LogManager().Insert(new Client_Log { Response = body, Error = "Body Deserialize fail move to line 141", timestamp = DateTime.Now, Message = "" });

                    }

                }



                _logger.LogInformation("**** MSDSL END LOG ****");
            }
            catch (Exception ex)
            {
                _logger.LogError( ex,ex.Message);
                                return StatusCode(500,ex);
                               
            }

            return Ok();

        }

     
    }

 

Youtube Link

Powered by Blogger.