我可以使用网络服务将用户选择的图像上传到服务器吗?

发布于 2025-01-08 15:32:48 字数 342 浏览 0 评论 0原文

我正在做一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉城凉梦凉人心 2025-01-15 15:32:48

如果您使用 asp:FileUpload 控件,则当用户单击“提交”时,您可以在页面的代码隐藏中执行类似以下操作:

protected void UploadButton_Click( object sender, EventArgs e ) {
    if ( !ImageUpload.HasFile ) {
        return;
    }

    string imageBase64 = System.Convert.ToBase64String( ImageUpload.FileBytes );

    YourService service = new YourService();

    service.UploadImage( imageBase64 );

这当然假设您的服务有一个名为 UploadImage 的方法,该方法接受一个字符串作为参数。

然后在后端,将字符串转换为字节数组:

byte[] imageArray = System.Convert.FromBase64String( base64ImageData );

并将其作为二进制数据保存到数据库中。

作为警告,您可能需要在网页上添加长度检查,以防止有人上传太大的图像......否则您的服务器可能会陷入困境。

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:

protected void UploadButton_Click( object sender, EventArgs e ) {
    if ( !ImageUpload.HasFile ) {
        return;
    }

    string imageBase64 = System.Convert.ToBase64String( ImageUpload.FileBytes );

    YourService service = new YourService();

    service.UploadImage( imageBase64 );

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:

byte[] imageArray = System.Convert.FromBase64String( base64ImageData );

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.

糖果控 2025-01-15 15:32:48

您应该使用 html 公式。但是您需要更改 form 标签的一些默认配置

<form action="url" method="post" enctype="multipart/form-data">
    <input type="file" name="my-file" />
</form>

,并且可以使用 iframe 作为表单的目标,而不是页面不会重新加载。然后您可以对 iframe 做出反应...

<form ... target="youriframe">

请参阅此网站获取完整示例

You should use a html formular. But you need to change some of the default configuration of the form-tag

<form action="url" method="post" enctype="multipart/form-data">
    <input type="file" name="my-file" />
</form>

and you can use an iframe as target of the form, than the page does not reload. You can react on the iframe then...

<form ... target="youriframe">

see this website for a full example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文