最近做一个项目,要把图片和视频传到服务器上(网站与图片服务器分开),在网上找了好久,没找到完整的资料。
自己也折腾了半天,才把完整的代码实现完。可能好多朋友都有实现过,没分享代码吧,写得不好希望不要见笑!!
public string UploadVide() { string requesturl = ""; string result = "video/Videoitem/"; HttpFileCollectionBase filetoUpload = Request.Files; foreach (string file in filetoUpload) { var curFile = Request.Files[file]; Stream sr= curFile.InputStream; byte[] filebyt = new byte[curFile.ContentLength]; Stream fileStream = curFile.InputStream;//建立文件流对象 fileStream.Read(filebyt,0,curFile.ContentLength); ServiceReference1.WebMp4serviceSoapClient sf = new ServiceReference1.WebMp4serviceSoapClient(); requesturl= sf.UpLoadStream(filebyt,curFile.FileName,0); line-height:1.5!important">D:\\Video\\"); } ServiceReference1.WebMp4serviceSoapClient sf = new ServiceReference1.WebMp4serviceSoapClient(); sf.u return requesturl; }
其次:webservice代码:
[WebMethod] string UpLoadStream(byte[] fs,string fileName,255); line-height:1.5!important">string requestPath) { try { string oldName = System.IO.Path.GetFileName(fileName); string expendName = System.IO.Path.GetExtension(oldName); string newName = DateTime.Now.ToString().Replace(" ",0); line-height:1.5!important">"").Replace(:-/""); ///定义并实例化一个内存流,以存放提交上来的字节数组 /// MemoryStream m = new MemoryStream(fs); 定义实际文件对象,保存上载的文件。 FileStream f = new FileStream(requestPath + newName + expendName,FileMode.Create); 把内内存里的数据写入物理文件 m.Writeto(f); m.Close(); f.Close(); f = null; m = null; return requestPath + newName + expendName; } catch (Exception error) { } return ""; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。