Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] attachFile results in corrupted Microsoft Word file #177

Open
dotwork opened this issue Mar 24, 2020 · 14 comments · Fixed by #199
Open

[Bug] attachFile results in corrupted Microsoft Word file #177

dotwork opened this issue Mar 24, 2020 · 14 comments · Fixed by #199
Assignees
Labels
bug Something isn't working

Comments

@dotwork
Copy link

dotwork commented Mar 24, 2020

Current behavior:

I'm having a problem with attachFile. When uploading a valid docx file, it works on my test server if I do it manually, but when cypress does it, I get an error about it being corrupted from the server-side software that ingests it. I've tried various encodings, as well as no encoding.

Here is the file in question:
valid_minimal.docx

This is the code I am using:
cy.get('input#ebook-upload-content').attachFile(pathToFile, {mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', encoding: 'base64'});
`
However, the error is being sent back from server-side software, Aspose Words, so unless you have access to that software, you will not receive the error I did. There is no issue ingesting and processing this file in our many other unit and functional tests, so it appears that the file upload process via Cypress specifically is changing the file somehow, in a way that corrupts it so that it cannot be processed by Aspose.

Versions

Cypress 4.2.0
cypress-file-upload 4.0.3
Chrome Version 80.0.3987.149 (Official Build) (64-bit)
Firefox 72.0.1 (64-bit)
Linux Mint

@dotwork dotwork added the bug Something isn't working label Mar 24, 2020
@dotwork
Copy link
Author

dotwork commented Mar 31, 2020

A bit more info...

When I read the file it is 18595 bytes, but when Cypress uploads, it has 31383 so for some reason it is almost doubling the length of the content. It has the same number of bytes for binary and base64 encodings, as well as with no provided encoding.

@dotwork
Copy link
Author

dotwork commented Mar 31, 2020

And a bit more info...
My boss ran a small file through it and compared the bytes to the original. It seems the one that went through cypress is filled with EF BF BD which is the dreaded question mark symbol:
https://apps.timwhitlock.info/unicode/inspect?s=%EF%BF%BD

image (18)

That explains why the uploaded version is almost twice the content length of the original.

@abramenal
Copy link
Owner

Hi, thanks for submitting the issue!

Right now I am not able to take a look into the issue properly.
In the meantime, I can suggest to check if the issue exist in v3 (previous major version):
https://github.com/abramenal/cypress-file-upload/tree/v3.5.3
Sorry about that. Not sure I can provide ETA on solving this issue in v4 since I am the only one contributor and maintainer. If you have ideas on fixing that, please feel free to open Pull Request and we can review it together.

Thanks for understanding.

@dotwork
Copy link
Author

dotwork commented Apr 9, 2020 via email

@abramenal
Copy link
Owner

@dotwork did you use same encoding in v3? I think it should be binary for any version

@dotwork
Copy link
Author

dotwork commented May 26, 2020

Yes, I have tried it with binary as well. I just happened to copy/paste the version where I was trying out base64 when I created the ticket. I have tried again with binary just now and confirmed again that this issue still exists and the new functionality is still corrupting files. Do you want to re-open this ticket? Should I submit a new one?

@abramenal abramenal reopened this May 26, 2020
@abramenal
Copy link
Owner

Okay, let's keep this issue then.
For a bit of context, the plugin does not modify file contents in any way. I suppose the problem will be fixed with using proper file encoding. Let me get back to this by the end of the week, I'm just not sure how to test that properly.

So you're saying you used 'base64' encoding in v3 and it worked correctly, right?

@dotwork
Copy link
Author

dotwork commented May 26, 2020

We are using v3.5.3 in our master branch, and yes we are using base64 encoding for these tests in Chrome and they all work great, testing .docx, .epub, .rft, .odt, .jpg, .zip. When I use binary in version 3 instead I get the error Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded..

As far as testing, I should think that if you upload a docx file, and then download the file you uploaded, the download should have the same content length as the original version you uploaded. If the downloaded version is longer (different), then it's still modifying the file during upload somehow. I've attached the docx file we use in testing.

valid_minimal.docx

@tkardos-vintra
Copy link

I am also experiencing the same issue as documented, with a mp4. File size will have nearly doubled upon attachment and the file
data passed is corrupt. I've passed it with default parameters, also as binary, also as video/mp4.

Cypress 4.9.0
Cypress-file-upload: 4.0.7

Implementation looks like this:

fileToUpload = fileToUpload + ".mp4";
cy.fixture(fileToUpload).then((fileContent) => {
// cy.get('input[type="file"]').attachFile(fileToUpload);
cy.get('input[type="file"]').attachFile({
fileContent: fileContent,
fileName: fileToUpload,
mimeType: "binary",
});
});

Have tried calling attachFile directly without the fixture call as well.

@spragya
Copy link

spragya commented Nov 19, 2020

I am seeing same issue with mp4 file upload. After upload file was corrupt . jpg and .pdf upload works fine.

@jerdaane
Copy link

jerdaane commented Dec 7, 2020

After looking at #70 I could workaround this issue (where in my case a pdf file was corrupted) by using base64 encoding

	cy.fixture('/test-files/my-file.pdf', 'base64').then((data) => {
		cy.get('input[type="file"]').attachFile({
			filePath: '/test-files/my-file.pdf',
			fileContent: data,
			fileName: 'my-file.pdf',
			encoding: 'base64',
			mimeType: 'application/octet-stream',
		});
	});

@spragya
Copy link

spragya commented Dec 9, 2020

I am seeing same issue with mp4 file upload. After upload file was corrupt . jpg and .pdf upload works fine.

cy.fixture('file_example.mp4', 'binary')
            .then(Cypress.Blob.binaryStringToBlob)
            .then(fileContent => {
                cy.get('[type="file"]').attachFile({
                    fileContent,
                    fileName: 'file_example.mp4',
                    mimeType: 'video/mp4',
                    encoding: 'utf8'
                })
            })

This worked for me. I am using Cypress : 6.0.0, cypress-file-upload: 4.1.1

@andrico1234
Copy link
Contributor

This worked for me when trying to test uploading an encrypted file

thanks @jerdaane

After looking at #70 I could workaround this issue (where in my case a pdf file was corrupted) by using base64 encoding

	cy.fixture('/test-files/my-file.pdf', 'base64').then((data) => {
		cy.get('input[type="file"]').attachFile({
			filePath: '/test-files/my-file.pdf',
			fileContent: data,
			fileName: 'my-file.pdf',
			encoding: 'base64',
			mimeType: 'application/octet-stream',
		});
	});

@ibenjelloun
Copy link

ibenjelloun commented Apr 14, 2022

Faced the issue while attaching a pptx file to input and adding the encoding fixed :

Failing code :

getInput().attachFile('file.pptx');

Working code :

getInput().attachFile({
          filePath: 'file.pptx',
          encoding: 'base64'
});

Edit sometime after :

After it worked for sometimes, its stopped working, can't make it work anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants