【问题标题】:uploading image from iphone with other data with validation using json wcf从 iphone 上传图像和其他数据并使用 json wcf 进行验证
【发布时间】:2014-11-01 04:46:41
【问题描述】:

我正在为一个 iphone 应用程序创建一个 wcf。我需要的是一个 POST,我将在其中使用图像和其他数据更新 sql。我知道如何获取数据,但我是更新图像的新手,有没有我可以浏览的示例并将其应用于我的项目。网上有很多 wcf 上传主题,但没有什么能接近我想要的。我所看到的只是上传到特定文件夹。我需要将它上传到sql。我对流一无所知。

using System.ServiceModel; using System.ServiceModel.Web; using System.IO;  namespace RESTImageUpload {   
    [ServiceContract]
    public interface IImageUpload
    {
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "FileUpload{empid},{fileName}")]
        void FileUpload(string empid,string fileName, Stream fileStream);         

    }  }

【问题讨论】:

  • 这是一个需求描述......

标签: c# sql json wcf image-uploading


【解决方案1】:

对我来说,以下人员工作(我将文件从剃刀视图上传到控制器操作,该操作连接到 WCF 服务并将文件作为字节传递)

您的运营合同:

     [ServiceContract]
     public interface IService1
     {
        [OperationContract]
        string FileUpload(byte[]buffer);
     }

您的服务实现文件:

  public class Service1 : IService1
  {
    public string FileUpload(byte[] buffer)
    {
        using (var connection = 
               new SqlConnection(ConfigurationManager.ConnectionStrings["myCnnStr"].ToString()))
        {
            var cmd = new SqlCommand(
                    "INSERT INTO Images (ImageData) VALUES(@buffer)",
                    connection);
            cmd.Parameters.Add(new SqlParameter("buffer", buffer));              
            connection.Open();
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception exc)
            {
                // log exception
                return "fail";
            }
       }
        return "ok";
    }
 }

WCF 应用程序的 web.config 文件中的设置:

         <bindings>
           <basicHttpBinding>
             <binding maxReceivedMessageSize="1000000000"></binding>
           </basicHttpBinding>
         </bindings>

您的 mvc 控制器接受文件上传并调用 WCF 服务函数 SaveFile

 public class HomeController : Controller
 {
    [HttpPost]
    public ActionResult Index(HttpPostedFileBase file)
    {
        var client = new Service1Client();          
        var buffer = new byte[file.ContentLength];
        file.InputStream.Read(buffer, 0, file.ContentLength);
        client.FileUpload(buffer);

        return View();
    }
}

【讨论】:

  • 谢谢,但我想从 iphone 应用程序中获取图像并保存到 sql 中
  • 我不认为有人会给你完整的解决方案。无论如何,我在 WCF SaveFile 函数中添加了代码,它可以让您将图像保存到 sql 数据库中。我还假设数据库中的 ImageData 列的 DataType 为 varbinary。
猜你喜欢
  • 2019-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多