微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MVC与webservice上传文件图片和视频,希望帮且到一些朋友

最近做一个项目,要把图片和视频传到服务器上(网站与图片服务器分开),在网上找了好久,没找到完整的资料。

自己也折腾了半天,才把完整的代码实现完。可能好多朋友都有实现过,没分享代码吧,写得不好希望不要见笑!!

下面贴代码吧:首先MVC代码

复制代码

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 "";
        }

返回
上传文件URL用于保存到数据库(根据你自己的需求来改)

HTML代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
< form  id="form1" action="/VideoAdmin/UploadVide" method="post" enctype="multipart/form-data">
                     table  cellspacing="0" cellpadding="0" border="0" class="tableadd">
                         tr >
                             td >
                                 选择视频:
</ >
>
input  id="btnfile" type="file" name="file">< id="txturl" type="text" name="txturl"
                                     value="D:" />< id="uploatvoide" type="submit" value="上传视频" />
>
>
>
>
视频名称
>
>
id="testvideoname" type="text" readonly="readonly" value="dddsds" />
>
>
                        
table >
form >

 

代码经过测试,是可以的,希望帮到大家,写得不好,还希望不要见笑!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐