加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 22:54:55 所属栏目:安全 来源:网络整理
导读:最近做一个项目,要把图片和视频传到服务器上(网站与图片服务器分开),在网上找了好久,没找到完整的资料。 自己也折腾了半天,才把完整的代码实现完。可能好多朋友都有实现过,没分享代码吧,写得不好希望不要见笑!! 下面贴代码吧:首先MVC代码: publi

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

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

下面贴代码吧:首先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 >

?

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读