使用json-wcf从iphone上传带有验证的其他数据的图像
本文关键字:验证 其他 数据 图像 json-wcf iphone 使用 | 更新日期: 2023-09-27 18:20:55
我正在为一个iphone应用程序创建一个wcf。我需要的是一个POST,在那里我将用图像和其他数据更新sql。我知道如何获取数据,但我是图像更新的新手。有什么例子我可以借鉴并应用到我的项目中吗。网上有很多wcf上传的话题,但没有一个是我想要的。我看到的只是上传到一个特定的文件夹。我需要把它上传到sql。我对stream一无所知。
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);
} }
对我来说,以下人员工作(我从剃刀视图上传一个文件到控制器操作,该操作连接到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>
接受文件上传并调用WCF服务函数SaveFile 的mvc控制器
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();
}
}