将图像保存到Web服务并取回它 [英] Save image to web service and get its back

查看:70
本文介绍了将图像保存到Web服务并取回它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个在线书店,上传图书的图书应该是通过XML网络服务

现在我找到了将图像保存在Web服务文件系统中的代码但是问题是

1 - 如何建立一个方法来取回图像的网址或图像自己

2 - 如何获取图像或图像网址和设置为asp.net图像控件或其他合适的控件



以下是Web服务接受参数的方法:1-字节数组形式的图像和2 - 文件名,此方法将图片保存在名为TransientStorage文件夹的文件夹中



I am building an online book store , the upload of the pictures of the books shall be through XML web service
Now I found the code to save the image at the web service file system but The Problem is
1 - how to Build a method to get back the url of the image or the image it self
2 - how to Get the image or the image url and set to asp.net image control or other suitable control

below is the method at the web service accept parameters : 1- the image in byte array form and the 2 - file name , this method save the picture in folder called "TransientStorage" folder

[WebMethod]
       public string UploadFile(byte[] f, string fileName)
       {
           // the byte array argument contains the content of the file
           // the string argument contains the name and extension
           // of the file passed in the byte array
           try
           {
               // instance a memory stream and pass the
               // byte array to its constructor
               MemoryStream ms = new MemoryStream(f);

               // instance a filestream pointing to the
               // storage folder, use the original file name
               // to name the resulting file
               FileStream fs = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath
                           ("~/TransientStorage/") + fileName, FileMode.Create);

               // write the memory stream containing the original
               // file as a byte array to the filestream
               ms.WriteTo(fs);

               // clean up
               ms.Close();
               fs.Close();
               fs.Dispose();

               // return OK if we made it this far
               return "OK";
           }
           catch (Exception ex)
           {
               // return the error message if the operation fails
               return ex.Message.ToString();
           }

推荐答案

嘿奥马尔,



如果我的问题正确,这就是解决方案。

如果您没有在网络服务上进行任何安全检查。



Hey Omar,

If i got your problem right, here is the solution.
Provided you have''nt kept any security check at the web-service.

引用:

如何构建一个方法来获取图像的url或者它自己的图像

how to Build a method to get back the url of the image or the image it self





您可以通过从上传文件方法而不是状态返回图片网址来取回图片网址。

并通过字符串函数或定期检查其状态表达式。





You can get back the image URL by returning it from Upload file method, instead of status.
And check its status through string functions or regular expressions.

[WebMethod]
       public string UploadFile(byte[] f, string fileName)
       {
           // the byte array argument contains the content of the file
           // the string argument contains the name and extension
           // of the file passed in the byte array
           try
           {
               // instance a memory stream and pass the
               // byte array to its constructor
               MemoryStream ms = new MemoryStream(f);

               // instance a filestream pointing to the
               // storage folder, use the original file name
               // to name the resulting file

               string imgURL=System.Web.Hosting.HostingEnvironment.MapPath
                           ("~/TransientStorage/") + fileName;

               FileStream fs = new FileStream(imgURL, FileMode.Create);

               // write the memory stream containing the original
               // file as a byte array to the filestream
               ms.WriteTo(fs);

               // clean up
               ms.Close();
               fs.Close();
               fs.Dispose();

               // return OK if we made it this far
               return imgURL;
           }
           catch (Exception ex)
           {
               // return the error message if the operation fails
               return ex.Message.ToString();
           }







Quote:

如何获取图像或图像网址并设置为asp.net图像控件或其他合适的控件

how to Get the image or the image url and set to asp.net image control or other suitable control





一旦获得img URL,您可以将其设置为图像控件或只是HTML图像标记,



< img src =imgURL/>



Once you get the img URL, you can set it to image control or simply an HTML image tag,

<img src="imgURL" />

这个问题可以通过两种不同的方式解决:

1-虚拟目录,Web服务可以保存图像,ASP.NET应用程序可以在图像的虚拟路径aspx页面上的虚拟目录和显示

2- Web服务将图像保存在目录中,ASP.NET应用程序中的通用处理程序将图像作为字节数组显示在aspx页面



下面提供的第二个解决方案提示&诀窍



将图像提交给Web服务并让他们回来了 [ ^ ]
This problem can be solved in two different ways :
1- Virtual directory where the web service can save the images and the ASP.NET application can ge the virtual path of the images at the virtual directory and display at the aspx page
2- Web sevice save the images at a directory and a generic handler at the ASP.NET application get the images as array of bytes to be displayed at aspx page

The second solution available at the below Tip & Trick

Submit Images to Web Service and Get Them Back[^]


这篇关于将图像保存到Web服务并取回它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆