如何保存网络服务返回的图像?
我有一个 Web 服务调用将返回一个图像,现在我想将此图像保存到服务器中的文件系统中。
问题是,我无法从服务器进行 Web 服务调用,因为 Web 服务应用程序在每个用户计算机上运行,并且以 http://localhost/get_image
形式向服务发出请求,该请求返回图像。
如何将该图像保存到服务器上?
I have a webservice call that will return an image, now I want to save this image to the filesystem in the server.
The problem is, I cannot make the webservice call from the server, as the webservice application runs on each user machine and a request is made to the service as http://localhost/get_image
, which returns the image.
How do I save this image on the server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 HTML5 使用 javascript 加载图像,并将 Base64 编码的响应发送到服务器,您可以在服务器中解码响应并将图像写入文件。 方法
确保 Web 服务响应标头具有“Access-Control-Allow-Origin: *”以允许交叉源资源共享
Jquery 代码
You can use HTML5 to load image using javascript and send base64 encoded response to sever where you can decode the response and write image to a file. Here is the approach
Ensure that the webservice response headers has "Access-Control-Allow-Origin: *" to allow cross origin resource sharing
Jquery code
我更喜欢使用 Carrierwave gem 进行 url 图像上传
http://railscasts.com/剧集/253-rierwave-file-uploads?autoplay=true
I prefer using carrierwave gem for url image upload
http://railscasts.com/episodes/253-carrierwave-file-uploads?autoplay=true
如上所述,通过网络传输图像时应使用 base64 编码。 Base64 表示 ASCII 字符串格式的二进制数据。它专门用于 MIME 内容传输编码和 XML 中的复杂数据存储。
您可以在这里尝试Javascript base64编码解码 http://rumkin.com/tools/compression/base64.php
此外,当您尝试写入 png 图像文件时,请确保使用 file.write 和 file.read 而不是 file.puts 和 file.gets
虽然最好的解决方案是将图像 URL 作为 Web 服务响应返回并获取来自资产的图像 服务器。
As suggested above base64 encoding should be used when transferring image over wire. Base64 represents binary data in an ASCII string format. Its specifically for MIME content transfer encoding and storing complex data in XML.
You can try Javascript base64 encoding decoding here http://rumkin.com/tools/compression/base64.php
Also, make sure you use file.write and file.read instead of file.puts and file.gets when you try to write png image files
Though the best solution is to return image URL as web service response and fetch the image from the asset server.