I'm trying to read an Excel file with extension .xlsx which is created using Spreadsheetgear2017 and I'm getting an exception stating Corrupt open XML file.
Here I'm sure that the sheet doesnt contain any Cell comments and form controls as the sheet itself was created using Spreadsheetgear2017.
I tried to save the file in .xls format but when I open the downloaded file, there is a popup stating "The file format and extension of filename.xls file don't match. The file could be corrupted."
Here I'm using GetWorkBook(value) method.
where am I missing out ?
Related
I have an excel file stored in src folder. I want to use react to read the excel file directly instead of uploading a file using input field and show the object array to the console, does anyone know how to do it? Any reference link about that? I saw a lot about uploading an excel file and output the data, but I don't want to upload the file. I want to import the file directly or fetch data from the file. Let me know if you know how to do in react. Thanks.
You could use FileReader API. Here's a link to similar question. You could parse/read the excel file using this package.
A little suggestion, would be to use .csv or .json files instead of .xlsx files, if you are only dealing in data.
fetch(excelFile).then((res) => res.arrayBuffer())
.then((ab) => {
const wb = XLSX.read(ab, { type: "array" });
const sheetname = wb.SheetNames[0]
const ws = wb.Sheets[sheetname]
const json = XLSX.utils.sheet_to_json(ws,{header: 1,defval:''})
console.log(json)
Here excelFile is file location, you may include your excel file location, Then json contain excel data.
Thanks.
I have a custom form using a "managed_file" which uploads to temp folder. Programmatically, I then load that file and move it to its permanent storage (overwriting any existing file with the* name) e.g.
// Upload file
$upfile = $this->entityTypeManager->getStorage('file')->load($fid);
// Source and destination
$sourceUri = $this->fileSystem->realpath($upfile->getFileUri());
$destinationUri = $this->fileSystem->realpath(\Drupal::config('system.file')->get('default_scheme') . "://") . '/x/y/z/XYZ_NEW.pdf';
// Move and overwrite
$this->fileSystem->move($sourceUri, $destinationUri, FileSystemInterface::EXISTS_REPLACE);
All of this works (i.e. the file physically is moved into the correct place with correct name); however, the file displayed in the listings (i.e. /admin/content/files) still shows the incorrect temporary folder as the URI.
Basically the file in the listings page seems to be showing the original filepath and name* of a previously successfully moved* file.
If I load this file with incorrect URI, i.e. using the incorrect temp path, the data loads, but then will not have a file info (as it doesn't exist. Also clicking the filename by browser under listings will show page not found and the URL showing the old URL i.e. /system/temporary?file=XYZ.pdf).
If I load this file by correct URI, i.e. using the correct destination path, file not found - same if I go to the path directly in the browser.
It appears the managed file doesn't know it was moved. How to resolve this bug?
The docs for FileSystem::move say "Moves a file to a new location without database changes or hook invocation."
So you are going to need to update the file entity with the new values..
Try this, untested:
// Upload file
$upfile = $this->entityTypeManager->getStorage('file')->load($fid);
// Source and destination
$sourceUri = $this->fileSystem->realpath($upfile->getFileUri());
$destinationUri = $this->fileSystem->realpath(\Drupal::config('system.file')->get('default_scheme') . "://") . '/x/y/z/XYZ_NEW.pdf';
// Move and overwrite
$newFileName = $this->fileSystem->move($sourceUri, $destinationUri, FileSystemInterface::EXISTS_REPLACE);
// Set the new file path on the file entity.
$upfile->setFileUri($newFileName);
// Set the file to permanent if needed.
$upfile->setPermanent();
// Save entity with changes.
$upfile->save();
I did not test this though.
You can check the functions on the file entity in the docs here
It turns out the class based methods do not update the database
https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21File%21FileSystem.php/function/FileSystem%3A%3Amove/8.9.x
The procedural version does
https://api.drupal.org/api/drupal/core%21modules%21file%21file.module/function/file_move/8.9.x
Currently I'm using form recognizer version 2.1 preview to train a custom model. I'm able to test the model in Form Recognizer Labeling Tool and got the output. When I input the same file that I got out in labeling tool in my program I'm getting the error below.
{"status": "failed", "createdDateTime": "2020-09-25T20:03:21Z", "lastUpdatedDateTime": "2020-09-25T20:03:21Z", "analyzeResult": {"version": "2.1.0", "errors": [{"code": "2005", "message": "The file submitted couldn't be parsed. This can be due to one of the following reasons: the file format is not supported ( Supported formats include JPEG, PNG, BMP, PDF and TIFF), the file is corrupted or password protected."}]}}
The GET request code used is:
resp = requests.get(url=get_url,headers={"Ocp-Apim-Subscription-Key":FORM_RECOGNIZER_SUBSCRIPTION_KEY})
I have saved the file to server and then tried to read it from there and pass the read file to form recognizer. This worked for me. But I don't know why did it happen.
I also encountered this exact error message following this article.
The article show 4 steps:
Train Model (require SAS to blob - whole folder)
Get model result
Analyze (require SAS to a single file)
Get analyze result
Profit
I got this error on step 4.
After monkeying around, I figured that the cause is not actually in step 4 but in step 3 instead. I was providing SAS to the blob instead of SAS to the file.
After I corrected the SAS URL it works perfectly.
Here is how to get SAS to blob:
Here is how to get SAS to a file:
What file are you trying to use as input ? Form Recognizer supports PDF, Tiff and images (PNG and JPEG) as file types and inputs to the analyze API. See more details here - https://learn.microsoft.com/en-us/azure/cognitive-services/form-recognizer/quickstarts/python-labeled-data?tabs=v2-0#analyze-forms-for-key-value-pairs-and-tables
is it possible, in odoo 10, to upload a file in the form of a given model ?
The user of the custom module has a list of items that he receives from is clients and he needs the invoices of thoses items. I imagine that this files are pdf files, but not necessarely (But I can tell to upload pdfs only if that is simple...).
The problem is, some times, in other technologies, a file is uploaded, but cannot be read when it is downloaded (some kind of corruption). So I really need to know how to do this in the sipliest and correct way.
Thank you So much for the help.
in your model's py file
upload_file = fields.Binary(string="Upload File")
file_name = fields.Char(string="File Name")
in your xml form view file:
<field name="upload_file" filename="file_name"/>
<field name="file_name" invisible="1"/>
after uploading and saving file the download button shows and file retrived in save format which is uploaded
Im using cakepdf for generating pdf files using wkhtmltopdf and it works, the issue is when i wanted to create pdf files inside a folder it shows me this exception :
System error <pre>Exit with code 1 due to network error: ContentNotFoundError </pre>
Here's the code Im using for generating the pdf file :
$CakePdf = new CakePdf();
$CakePdf->template('mynote','default');
//get the pdf string returned
$pdf = $CakePdf->output();
//or write it to file directly
$pdf = $CakePdf->write(APP . 'webroot'. DS .'files' . DS . 'mynote.pdf');
and for the pdf template i used mynote.ctp that only contains this code inside pdf folder
<div>Hello</div>
After some hustle i disbaled this error by commenting the code inside output functions but still i can't get the style and the images,like i use to.
Thanks in advance.