我可以使用网络服务将用户选择的图像上传到服务器吗?
我正在做一个 ASP .NET 网站,其中用户可以上传需要保存到服务器的图像。
在整个网站中,我试图尽可能减少客户端和服务器之间的通信。这意味着在 onload() 上我调用 Web 服务,该服务返回该页面所需的所有数据,然后使用 javascript 操作它们。
到目前为止,它运行得完美无缺。当用户希望更改其个人资料时就会出现问题。我可以获取文本字段中输入的所有信息并进行排序,并将它们作为参数传递给 Web 服务,该服务将它们保存到数据库中。我不知道该怎么做,或者如果可能的话,是将所选图像作为参数传递给网络服务。
我使用 html5: 进行图像选择
I am doing a ASP .NET website where among other things a user can upload an image, that needs to be saved to the server.
In the whole website I'm trying to accomplish as minimal communication between the client and server as possible. This means that on onload() I invoke web services which return all data needed for that page, and then manipulate them using javascript.
So far, it has worked flawlessly. The problem arises when a user wishes to make changes to his profile. I can take all the information entered in the text fields and the sort, and pass them as arguments to a webservice which saves them to the database. The thing I dont know how to do, or if it is even possible, is to pass the selected image as an argument to the webservice.
I am using html5: <input type="file">
for the image selection
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用
asp:FileUpload
控件,则当用户单击“提交”时,您可以在页面的代码隐藏中执行类似以下操作:这当然假设您的服务有一个名为 UploadImage 的方法,该方法接受一个字符串作为参数。
然后在后端,将字符串转换为字节数组:
并将其作为二进制数据保存到数据库中。
作为警告,您可能需要在网页上添加长度检查,以防止有人上传太大的图像......否则您的服务器可能会陷入困境。
If you use the
asp:FileUpload
control instead, when the user clicks submit you can do something similar to the following in the page's code-behind:This assumes of course that your service has a method called UploadImage that takes a string as a parameter.
Then on the back end, convert the string to a byte array:
And save it as binary data to your database.
As a warning, you may want to add length checks on the webpage to prevent someone from uploading images that are too large... otherwise your server could be bogged down.
您应该使用 html 公式。但是您需要更改
form
标签的一些默认配置,并且可以使用 iframe 作为表单的目标,而不是页面不会重新加载。然后您可以对 iframe 做出反应...
请参阅此网站获取完整示例
You should use a html formular. But you need to change some of the default configuration of the
form
-tagand you can use an iframe as target of the form, than the page does not reload. You can react on the iframe then...
see this website for a full example