webpack 不支持React Url方案c

zf2sa74q  于 2023-10-19  发布在  Webpack
关注(0)|答案(1)|浏览(166)

我目前正在开发一个react web应用程序,其中我有一个输入类型“file”,用户应该使用它来上传图像。输入本身起作用,这意味着当单击按钮时,文件浏览器打开,可以选择文件。然而,在此之后,控制台抛出一个错误,其中指出:
Fetch API无法加载c:\fakepath\freies_projekt-17.jpg。不支持URL方案“c”。
Input作为Array of Inputs的一部分呈现,并具有以下属性。

  1. {
  2. name: "image",
  3. component: (
  4. <Input type={"file"} accept="image/png, image/jpeg" name="image" />
  5. )
  6. },

使用这些属性,将呈现以下组件。

  1. export default function Input({ type = "number", name, value, key }) {
  2. const { updateSettings } = useSettingsStore();
  3. return (
  4. <StyledLabel>
  5. <StyledInput
  6. type={type}
  7. onChange={e => updateSettings(e)}
  8. value={value}
  9. />
  10. {name}
  11. </StyledLabel>
  12. );
  13. }

选择文件后,该文件的路径将存储在设置存储中。然后将其用于p5组件中

  1. export default function Canvas() {
  2. let tileWidth;
  3. let img;
  4. const settings = useSettingsStore();
  5. const image = useSettingsStore(state => state.image);
  6. const { canvasSize, bgColor, tiles } = settings;
  7. console.log(settings.image);
  8. const preload = p => {
  9. img = p.loadImage(image);
  10. };
  11. const setup = (p, canvasParentRef) => {
  12. p.createCanvas(canvasSize.w, canvasSize.h).parent(canvasParentRef);
  13. p.strokeCap(p.SQUARE);
  14. p.pixelDensity(4);
  15. };
  16. const draw = p => {
  17. if (img == undefined) {
  18. img = p.loadImage(image);
  19. }
  20. img.resize(p.width, p.height);
  21. p.background(bgColor);
  22. tileWidth = p.width / tiles;
  23. for (let y = 0; y < tiles; y++) {
  24. for (let x = 0; x < tiles; x++) {
  25. let c = img.get(x * tileWidth, y * tileWidth);
  26. let b = p.brightness(c);
  27. //lineRaster(x, y, b);
  28. borderRadiusRaster(p, x, y, b);
  29. }
  30. }
  31. };

这个项目是在一段时间前开始的,所以有些东西可能已经过时了。我正在使用以下加载器处理node v16.13.0和webpack:

  1. module: {
  2. rules: [
  3. {
  4. test: /\.js$/,
  5. exclude: /node_modules/,
  6. use: {
  7. loader: 'babel-loader',
  8. },
  9. },
  10. {
  11. test: /\.(png|jpe?g|gif)$/i,
  12. use: [
  13. {
  14. loader: 'file-loader',
  15. },
  16. ],
  17. },
  18. ],
  19. },

我猜解决方案就在我的webpack配置中,但我不确定从哪里开始寻找。
如果有人能帮我解决这个问题,我会很高兴的,thx:)

iecba09b

iecba09b1#

它显示错误,因为文件显示您的位置系统的位置。网上没有所以,要做到这一点,你有两个选择。一个是使用url而不是文件输入,第二个是在代码中使用firebase数据库。你会得到firebase网站的适当文档,说明如何使用firebase存储通过输入上传文件,如果你这样做,你的文件现在在网络上,而不是本地机器上。

相关问题