Click here to Skip to main content
14,698,496 members
Home » Discussions » JavaScript
   

JavaScript

 
GeneralRe: Can ms blazor be next level of client-side coding Pin
simpledeveloper5-Nov-19 7:09
Membersimpledeveloper5-Nov-19 7:09 
GeneralRe: Can ms blazor be next level of client-side coding Pin
Pete O'Hanlon5-Nov-19 21:40
subeditorPete O'Hanlon5-Nov-19 21:40 
QuestionOpen base64 encoded pdf file using javascript Pin
Member 1286358021-Oct-19 5:16
MemberMember 1286358021-Oct-19 5:16 
AnswerRe: Open base64 encoded pdf file using javascript Pin
jkirkerx21-Oct-19 13:22
professionaljkirkerx21-Oct-19 13:22 
AnswerRe: Open base64 encoded pdf file using javascript Pin
Richard Deeming22-Oct-19 2:15
mveRichard Deeming22-Oct-19 2:15 
GeneralRe: Open base64 encoded pdf file using javascript Pin
jkirkerx24-Oct-19 13:18
professionaljkirkerx24-Oct-19 13:18 
AnswerRe: Open base64 encoded pdf file using javascript Pin
Parth Munjpara23-Oct-19 0:11
MemberParth Munjpara23-Oct-19 0:11 
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 
I have a Web Api that is returning FileStreamResult, I want to download the file that's in zip format, I could able to download if Web Api is returning the raw byte array, but for some reason lead wanted it only from FileStreamResult, can somebody please help me how can I write to a file using the FileStreamResult - thank you. I am putting the code here, the Web Api method which is returning as FileStreamResult, but the client code of javascript functions and react events I am putting which is working but with bytes array, so that somebody can suggest me what modifications I can make to the client script to make work and download the zip file. I need some help please - thank you.
Below is my Web Api Code:

[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;

	byte[] theZipFile = null;
	FileStreamResult result = 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();
		result = new FileStreamResult(zipStream, "application/zip") { FileDownloadName = $"{communityName}.zip" };
	}

	//return theZipFile;
	return result;
    }

Here is my React event for the download

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: 'application/blob',
		contentType: 'application/blob',
		url: url,
		data: formData
	})
		.then(res => {
			console.log(res);
			console.log(res.data);
			let fileName = `${this.state['selectedCommunity']}`
			const arrayBuffer = base64ToArrayBuffer(res.data);
			createAndDownloadBlobFile(arrayBuffer, fileName, 'zip');
		})
		.catch(error => {
			console.log(error.message);
		});
    };

Here are the common fuctions I have written in CommonFuncs.js file:

export function createAndDownloadBlobFile(body, filename, extension = 'pdf') {
    const blob = new Blob([body]);
    const fileName = `${filename}.${extension}`;
    if (navigator.msSaveBlob) {
        // IE 10+
        navigator.msSaveBlob(blob, fileName);
    } else {
        const link = document.createElement('a');
        // Browsers that support HTML5 download attribute
        if (link.download !== undefined) {
            const url = URL.createObjectURL(blob);
            link.setAttribute('href', url);
            link.setAttribute('download', fileName);
            link.style.visibility = 'hidden';
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
        }
    }
    }

    export function base64ToArrayBuffer(base64) {
        const binaryString = window.atob(base64); // Comment this if not using base64
        const bytes = new Uint8Array(binaryString.length);
        return bytes.map((byte, i) => binaryString.charCodeAt(i));
    }

Any help would be very helpful please- thanks in advance

modified 17-Oct-19 19:45pm.

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 
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
mveRichard 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
mveRichard 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
mveRichard Deeming16-Oct-19 8:49 

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.