Click here to Skip to main content
14,454,024 members

JavaScript

 
QuestionWant to download a zip file that's returned as FileStreamResult only, I could able to download in-memory using byte array but Pin
simpledeveloper17-Oct-19 14:27
Membersimpledeveloper17-Oct-19 14:27 
AnswerRe: Want to download a zip file that's returned as FileStreamResult only, I could able to download in-memory using byte array but Pin
jkirkerx21-Oct-19 13:11
professionaljkirkerx21-Oct-19 13:11 
GeneralRe: Want to download a zip file that's returned as FileStreamResult only, I could able to download in-memory using byte array but Pin
simpledeveloper21-Oct-19 14:16
Membersimpledeveloper21-Oct-19 14:16 
GeneralRe: Want to download a zip file that's returned as FileStreamResult only, I could able to download in-memory using byte array but Pin
jkirkerx22-Oct-19 8:21
professionaljkirkerx22-Oct-19 8:21 
GeneralRe: Want to download a zip file that's returned as FileStreamResult only, I could able to download in-memory using byte array but Pin
simpledeveloper22-Oct-19 9:56
Membersimpledeveloper22-Oct-19 9:56 
GeneralRe: Want to download a zip file that's returned as FileStreamResult only, I could able to download in-memory using byte array but Pin
jkirkerx23-Oct-19 7:55
professionaljkirkerx23-Oct-19 7:55 
GeneralRe: Want to download a zip file that's returned as FileStreamResult only, I could able to download in-memory using byte array but Pin
simpledeveloper24-Oct-19 14:25
Membersimpledeveloper24-Oct-19 14:25 
Questionhas been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource Pin
simpledeveloper15-Oct-19 14:05
Membersimpledeveloper15-Oct-19 14:05 
I am calling the a Web Api from the my react component using fetch, when I used to run it as one application, there was no problem, but when I am running the application react separate from api, I am getting the CORS error, my fetch call is as below,

componentDidMount() {
        console.log(clientConfiguration)
        
        fetch(clientConfiguration['communitiesApi.local'])
            .then((response) => {
                return response.json();                
            })
            .then(data => {
                console.log(data);
                let communitiesFromApi = data.map(community => { return { value: community, display: community } });
                this.setState({ communities: [{ value: '', display: 'Select a Community...' }].concat(communitiesFromApi) });    
                            
            }).catch(error => {
                console.log(error);
            });
    };


and my post call using axios as below also.

handleDownload = (e) => {
        e.preventDefault();

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

        let url = clientConfiguration['filesApi.local'];
        let tempFiles = clientConfiguration['tempFiles.local'];

        axios({
            method: 'post',
            responseType: 'application/zip',
            contentType: 'application/zip',
            url: url,
            data: formData
        })
            .then(res => {       
                var fileName = `${this.state['selectedCommunity']}.zip`;
                saveAs(`https://localhost:44352/TempFiles/${res.data}`, fileName);
            });
    };


Here is my api code:
[HttpGet("{communityName}")]
public string Get(string communityName)
{
    string rootPath = Configuration.GetValue<string>("ROOT_PATH");
    string communityPath = rootPath + "\\" + communityName;

    string[] files = Directory.GetFiles(communityPath);

    List<string> strippedFiles = new List<string>();
    foreach (string file in files)
    {
        strippedFiles.Add(file.Replace(communityPath + "\\", ""));
    }

    return JsonConvert.SerializeObject(strippedFiles);
}

[HttpPost]
public string Post([FromForm] string communityName, [FromForm] string files) //FileContentResult
{
    var removedInvalidCharsFromFileName = removeInvalidCharsFromFileName(files);
    var tFiles = removedInvalidCharsFromFileName.Split(',');
    string rootPath = Configuration.GetValue<string>("ROOT_PATH");
    string communityPath = rootPath + "\\" + communityName;

    byte[] theZipFile = null;

    using (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);
                }
            }
        }

        theZipFile = zipStream.ToArray();
    }

    ////return File(theZipFile, "application/zip", communityName + ".zip");

    string tempFilesPath = Configuration.GetValue<string>("Temp_Files_Path");

    if (!System.IO.Directory.Exists(tempFilesPath))
        System.IO.Directory.CreateDirectory(tempFilesPath);

    System.IO.File.WriteAllBytes($"{tempFilesPath}\\{communityName}.zip", theZipFile);

    //return System.IO.File.ReadAllBytes($"{tempFilesPath}\\Test.zip");

    //return $"{tempFilesPath}\\{communityName}.zip";
    return $"{communityName}.zip";
}


And I am getting the error for Get as below: "
Access to fetch at 'https://localhost:44368/api/communities' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
"

modified 15-Oct-19 19:24pm.

AnswerRe: has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource Pin
jkirkerx15-Oct-19 14:54
professionaljkirkerx15-Oct-19 14:54 
AnswerRe: has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource Pin
F-ES Sitecore15-Oct-19 23:14
mveF-ES Sitecore15-Oct-19 23:14 
AnswerRe: has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource Pin
simpledeveloper16-Oct-19 8:10
Membersimpledeveloper16-Oct-19 8:10 
QuestionTrying to download Zip files that's returned from Api React Pin
simpledeveloper14-Oct-19 10:00
Membersimpledeveloper14-Oct-19 10:00 
AnswerRe: Trying to download Zip files that's returned from Api React Pin
Richard Deeming14-Oct-19 10:38
communityengineerRichard Deeming14-Oct-19 10:38 
GeneralRe: Trying to download Zip files that's returned from Api React Pin
simpledeveloper14-Oct-19 12:45
Membersimpledeveloper14-Oct-19 12:45 
GeneralRe: Trying to download Zip files that's returned from Api React Pin
Richard Deeming15-Oct-19 2:24
communityengineerRichard Deeming15-Oct-19 2:24 
GeneralRe: Trying to download Zip files that's returned from Api React Pin
simpledeveloper15-Oct-19 7:31
Membersimpledeveloper15-Oct-19 7:31 
GeneralRe: Trying to download Zip files that's returned from Api React Pin
simpledeveloper15-Oct-19 9:59
Membersimpledeveloper15-Oct-19 9:59 
GeneralRe: Trying to download Zip files that's returned from Api React Pin
Richard Deeming16-Oct-19 8:49
communityengineerRichard Deeming16-Oct-19 8:49 
GeneralRe: Trying to download Zip files that's returned from Api React Pin
simpledeveloper17-Oct-19 7:53
Membersimpledeveloper17-Oct-19 7:53 
GeneralRe: Trying to download Zip files that's returned from Api React Pin
Richard Deeming17-Oct-19 8:12
communityengineerRichard Deeming17-Oct-19 8:12 
GeneralRe: Trying to download Zip files that's returned from Api React Pin
simpledeveloper17-Oct-19 12:40
Membersimpledeveloper17-Oct-19 12:40 
GeneralRe: Trying to download Zip files that's returned from Api React Pin
Richard Deeming18-Oct-19 2:12
communityengineerRichard Deeming18-Oct-19 2:12 
GeneralRe: Trying to download Zip files that's returned from Api React Pin
simpledeveloper20-Oct-19 15:38
Membersimpledeveloper20-Oct-19 15:38 
GeneralRe: Trying to download Zip files that's returned from Api React Pin
simpledeveloper21-Oct-19 9:08
Membersimpledeveloper21-Oct-19 9:08 
GeneralRe: Trying to download Zip files that's returned from Api React Pin
simpledeveloper21-Oct-19 11:09
Membersimpledeveloper21-Oct-19 11:09 

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.