Click here to Skip to main content
14,662,527 members
Home » Discussions » JavaScript
   

JavaScript

 
GeneralRe: Trying to download Zip files that's returned from Api React Pin
simpledeveloper17-Oct-19 6:53
Membersimpledeveloper17-Oct-19 6:53 
GeneralRe: Trying to download Zip files that's returned from Api React Pin
Richard Deeming17-Oct-19 7:12
mveRichard Deeming17-Oct-19 7:12 
GeneralRe: Trying to download Zip files that's returned from Api React Pin
simpledeveloper17-Oct-19 11:40
Membersimpledeveloper17-Oct-19 11:40 
GeneralRe: Trying to download Zip files that's returned from Api React Pin
Richard Deeming18-Oct-19 1:12
mveRichard Deeming18-Oct-19 1:12 
GeneralRe: Trying to download Zip files that's returned from Api React Pin
simpledeveloper20-Oct-19 14:38
Membersimpledeveloper20-Oct-19 14:38 
GeneralRe: Trying to download Zip files that's returned from Api React Pin
simpledeveloper21-Oct-19 8:08
Membersimpledeveloper21-Oct-19 8:08 
GeneralRe: Trying to download Zip files that's returned from Api React Pin
simpledeveloper21-Oct-19 10:09
Membersimpledeveloper21-Oct-19 10:09 
AnswerRe: Trying to download Zip files that's returned from Api React Pin
simpledeveloper21-Oct-19 8:10
Membersimpledeveloper21-Oct-19 8:10 
Finally implemented by using the FileStreamResult as well maybe some people would be needed this, here is my API code and then I made call to the post method using axios, so here is my React code. In the axios call responseType becomes arraybuffer and the in the blob declaration it becomes the application/octet-stream type, Hence it completes everything, as I have imported the file-saver, I could able to use saveAs method of it. Finally after many efforts and hearing the screaming from PM, yes it is achieved - but that's the life of any Software Programmer.
Here is Web Api code C#:
[EnableCors("AnotherPolicy")]
        [HttpPost]
        public FileStreamResult Post([FromForm] string communityName, [FromForm] string files) //byte[]
        {
            var removedInvalidCharsFromFileName = removeInvalidCharsFromFileName(files);
            var tFiles = removedInvalidCharsFromFileName.Split(',');
            string rootPath = Configuration.GetValue<string>("ROOT_PATH");
            string communityPath = rootPath + "\\" + communityName;

            MemoryStream zipStream = new MemoryStream();

            using (ZipArchive zip = new ZipArchive(zipStream, ZipArchiveMode.Create, true))
            {
                foreach (string attachment in tFiles)
                {
                    var zipEntry = zip.CreateEntry(attachment);

                    using (FileStream fileStream = new FileStream(communityPath + "\\" + attachment, FileMode.Open))
                    {
                        using (Stream entryStream = zipEntry.Open())
                        {
                            fileStream.CopyTo(entryStream);
                        }
                    }
                }
            }

            zipStream.Position = 0;

            return File(zipStream, "application/octet-stream");
        }

Then my client side React code is here:
handleDownload = (e) => {
        e.preventDefault();

        var formData = new FormData();
        formData.append('communityname', this.state.selectedCommunity);
        formData.append('files', JSON.stringify(this.state['checkedFiles']));

        //let env='local';        
        let url = clientConfiguration['filesApi.local'];
        //let tempFiles = clientConfiguration[`tempFiles.${env}`];
        //alert(tempFiles);

        axios({
            method: 'post',
            responseType: 'arraybuffer', //Force to receive data in a Blob Format
            url: url,
            data: formData
        })
            .then(res => {
                let extension = 'zip';
                let tempFileName = `${this.state['selectedCommunity']}`
                let fileName = `${tempFileName}.${extension}`;

                const blob = new Blob([res.data], {
                    type: 'application/octet-stream'
                })

                saveAs(blob, fileName)
            })
            .catch(error => {
                console.log(error.message);
            });
    };

this event is called when button is clicked or form submitted. Thanks for all the support the SO has given - thanks a lot.
QuestionAppending an Array of strings is giving me objects instead of strings separated with commas, my code and results are as below Pin
simpledeveloper14-Oct-19 6:32
Membersimpledeveloper14-Oct-19 6:32 
AnswerRe: Appending an Array of strings is giving me objects instead of strings separated with commas, my code and results are as below Pin
Richard Deeming14-Oct-19 7:32
mveRichard Deeming14-Oct-19 7:32 
GeneralRe: Appending an Array of strings is giving me objects instead of strings separated with commas, my code and results are as below Pin
simpledeveloper14-Oct-19 8:18
Membersimpledeveloper14-Oct-19 8:18 
QuestionCalling Api post method on button click in React Pin
simpledeveloper10-Oct-19 12:11
Membersimpledeveloper10-Oct-19 12:11 
AnswerRe: Calling Api post method on button click in React Pin
jkirkerx10-Oct-19 13:40
professionaljkirkerx10-Oct-19 13:40 
GeneralRe: Calling Api post method on button click in React Pin
simpledeveloper10-Oct-19 15:18
Membersimpledeveloper10-Oct-19 15:18 
QuestionI am trying to read from a Web api call and assign it to a state in React component Pin
simpledeveloper9-Oct-19 11:31
Membersimpledeveloper9-Oct-19 11:31 
AnswerRe: I am trying to read from a Web api call and assign it to a state in React component Pin
jkirkerx10-Oct-19 13:31
professionaljkirkerx10-Oct-19 13:31 
Questionprogramming a dropdown menu to display specific points in the charts Pin
Gibby905-Oct-19 6:41
MemberGibby905-Oct-19 6:41 
QuestionSearch Engine without DB using JS Pin
javadev20904-Oct-19 9:06
Memberjavadev20904-Oct-19 9:06 
AnswerRe: Search Engine without DB using JS Pin
jkirkerx8-Oct-19 13:45
professionaljkirkerx8-Oct-19 13:45 
GeneralRe: Search Engine without DB using JS Pin
javadev20908-Oct-19 19:44
Memberjavadev20908-Oct-19 19:44 
GeneralRe: Search Engine without DB using JS Pin
jkirkerx9-Oct-19 7:35
professionaljkirkerx9-Oct-19 7:35 
Questionjquery validation in drop down Pin
Shraddha_Patel3-Oct-19 18:50
MemberShraddha_Patel3-Oct-19 18:50 
AnswerRe: jquery validation in drop down Pin
ZurdoDev4-Oct-19 1:04
mveZurdoDev4-Oct-19 1:04 
AnswerRe: jquery validation in drop down Pin
jkirkerx8-Oct-19 13:47
professionaljkirkerx8-Oct-19 13:47 
QuestionHow to display a Javascript alert and display a iframe using a javascript dynamic drop-down list Pin
codeguy13-Oct-19 19:14
Membercodeguy13-Oct-19 19:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.