以编程方式从 chrome 扩展上传文件。如何?

Programmatically upload a file from chrome extension. How to?

我需要从 chrome 扩展程序将本地文件上传到远程服务器。扩展名扩展 chrome 文件浏览器,我想在用户 'opens' 带有我的扩展名的文件时调用上传。 我原本希望使用套接字 API,但才意识到(很难)这些 API 仅在应用程序中可用。

有人可以推荐最好的 practice/approach 吗?

谢谢。

使用 HTTP PUT 或 POST,与从网络应用程序上传的方式相同。从 Javascript 开始,您可以使用 XMLHttpRequest(), specifying 'PUT' or 'POST' as the first argument to the open() method (whatever your server requires). MDN has this example 执行此操作(正如他们指出的那样,示例中的 sendAsBinary() 应该更改为简单的 send())。