react-file-viewer icon indicating copy to clipboard operation
react-file-viewer copied to clipboard

Getting No 'Access-Control-Allow-Origin' header issue while open file viewer

Open lakshmisanthi opened this issue 5 years ago • 17 comments

Hi, I am new to react-file-viewer. I want to show view some images and documents in my application. so I have installed the react-file-viewer package. here is my sample code

import React, { Component } from 'react'; import {Container, Modal,Button} from 'semantic-ui-react'; import FileViewer from 'react-file-viewer';

const file = 'https://storage.googleapis.com/dwellingg-dev-site-image/default_images/home.jpg' const type = 'jpg'

export default class Hello extends Component {

constructor(props) { 
super(props);
this.state ={ 
  showModal:false,
}   

} onError(e) { console.log(e, 'error in file-viewer'); } render() { return (
<Container> <Button onClick={()=>{this.setState({showModal:true})}}>View</Button> <Modal open={this.state.showModal} basic size='large' onClose={()=>{this.setState({showModal:false})}}> <Modal.Content> <FileViewer fileType={type} filePath={file}/> </Modal.Content> </Modal> </Container>
); } };

I getting below error.

Access to Image at 'https://storage.googleapis.com/dwellingg-dev-site-image/default_images/home.jpg' from origin 'http://192.168.1.4:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.1.4:3000' is therefore not allowed access.

Thanks in advance

lakshmisanthi avatar Oct 31 '19 06:10 lakshmisanthi

+1

akhilaudable avatar Nov 09 '19 07:11 akhilaudable

+1

YakimchykNadzeya avatar Nov 25 '19 08:11 YakimchykNadzeya

I am having the same CORS issue. Can somebody please provide an update or a workaround for this issue please?

Issue is only with jpg files the viewer will render a pdf document from the same location

paul-altyfc avatar Jan 29 '20 21:01 paul-altyfc

+1

iequivocality avatar Jun 23 '20 15:06 iequivocality

+1

Otherwise, the server hosted image file loads in a simple <img src={}/> but not <FileViewer/>

leewenjie avatar Jul 18 '20 08:07 leewenjie

I am having same issue now. What does mean of "+1"? Is this a solution? Please help me.

msilucifer avatar Nov 16 '20 06:11 msilucifer

Thanks, @yangchih But in my thought, it is not a good solution to change the file in node_modules folder. Do you have any other ideas?

msilucifer avatar Nov 17 '20 02:11 msilucifer

Thanks, @yangchih But in my thought, it is not a good solution to change the file in node_modules folder. Do you have any other ideas?

Although I add new line to code, it doesn't work :(

hxtruong6 avatar Dec 12 '20 09:12 hxtruong6

Any updates on this issue? Or any other package you guys could recommend?

anasyo10 avatar Jan 13 '21 12:01 anasyo10

I also have the same problem when I try to load a file from an external server.

geekl1 avatar Mar 07 '21 01:03 geekl1

Is this problem resolved? It doesn't still work for me. Or should I don't use this library?

visisemanaj avatar May 03 '21 17:05 visisemanaj

Is this problem resolved? It doesn't still work for me. Or should I don't use this library?

finally i switched to something else since its not working.

geekl1 avatar May 04 '21 04:05 geekl1

Is this problem resolved? It doesn't still work for me. Or should I don't use this library?

finally i switched to something else since its not working.

what did you use now ?

AlwaniAnis avatar Sep 14 '21 05:09 AlwaniAnis

@geekl1 @AlwaniAnis have you found an alternative or solution? Regards!

emiliobasualdo avatar Sep 14 '21 16:09 emiliobasualdo

If you are using gcloud storage then try to setup cors there

  1. install gcloud SDK
  2. Create json file (cors.json)
[
    {
      "origin": ["*"],
      "method": ["GET"],
      "responseHeader": ["Content-Type"],
      "maxAgeSeconds": 3600
    }
]
  1. Now config
gsutil cors set cors.json gs://BUCKET_NAME

check official docs

Iamsdt avatar Jan 07 '22 04:01 Iamsdt

still struggling by this

tariq3372 avatar Oct 24 '22 14:10 tariq3372

If you are using gcloud storage then try to setup cors there

  1. install gcloud SDK
  2. Create json file (cors.json)
[
    {
      "origin": ["*"],
      "method": ["GET"],
      "responseHeader": ["Content-Type"],
      "maxAgeSeconds": 3600
    }
]
  1. Now config
gsutil cors set cors.json gs://BUCKET_NAME

check official docs

https://stackoverflow.com/questions/37760695/firebase-storage-and-access-control-allow-origin

BossBele avatar Jun 14 '23 18:06 BossBele