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

文件上传的方法

 一。利用webservice上传文件

客户端:
页面:加入一个FileUpload 控件,一个Button(btnUpload)
下面是btnUpload的事件:

  protected   void  btnUpload_Click( object  sender, EventArgs e)

    
{    

        
////获得上传文件名称

        //FileInfo file = new FileInfo(fileUpload.PostedFile.FileName);


        
//上传文件转换为二进制流

        byte[] fileContent = fileUpload.FileBytes;

        
//获得上传文件名称

        string fileName = fileUpload.FileName;

        
//实例化webservice

        AdService.Service adTemp = new AdService.Service();

        
if (adTemp.UploadFile(fileContent, fileName)) //调用上传方法

        {

            Response.Write(
"OK");

        }

        
else

        
{

            Response.Write(
"error");

        }


    }


新建一个webService 项目,以下是上传方法代码

/// <summary>

    
/// 通过WebService上传文件

    
/// </summary>

    
/// <param name="fs">文件二进制流</param>

    
/// <param name="fileName">文件</param>

    
/// <returns></returns>

    [WebMethod(Description  =   " web提供的方法上传文件到相应的地址 " )]    

    
public   bool  UploadFile( byte [] fs,  string  fileName)

    
{

        
try

        
{

            
///定义并实例化一个内存流,以存放提交上来的字节数组。

            System.IO.MemoryStream m = new System.IO.MemoryStream(fs);

            
//取出存放地址,可以通过数据库里存放,不用定死了。此处只是做DEMO。

            string strFile = "E:" + "//" + "Personal files" + "//" + "good things" + "//" +fileName;

            
///定义实际文件对象,保存上载的文件

            System.IO.FileStream fl = new System.IO.FileStream(strFile, FileMode.OpenorCreate);

            
///把内内存里的数据写入物理文件

            m.Writeto(fl);            

            m.Close();

            fl.Close();

            m 
= null;

            fl 
= null;

            
return true;

        }

        
catch

        
{

            
return false;

        }


    }

 

二。利用控件上传文件

.aspx页面代码

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<script runat="server">
   
</script>

<body>
    <form id="form1" runat="server">
    <div>
        <ASP:FileUpload ID="FileUpload1" runat="server" /><br />
<br />
<ASP:Button ID="Button1" runat="server" OnClick="Button1_Click"
 Text="Upload File" /> <br />
<br />
<ASP:RegularExpressionValidator
 id="RegularExpressionValidator1" runat="server"
 ErrorMessage="Only mp3,m3u or mpeg files are allowed!"

//下面的文件类型帅选代码,可以在aspx.cs页面中写更为妥当
 ValidationExpression="^(([a-zA-Z]:)|(//{2}/w+)/$?)(//(/w[/w].*))+(.jpg|.JPG|.mp3|.MP3|.mpeg|.MPEG|.m3u|.M3U)$"
 ControlTovalidate="FileUpload1"></ASP:RegularExpressionValidator>
<br />
<ASP:requiredFieldValidator
 id="requiredFieldValidator1" runat="server"
 ErrorMessage="This is a required field!"
 ControlTovalidate="FileUpload1"></ASP:requiredFieldValidator>


        <br />
        <ASP:Label ID="Label1" runat="server"></ASP:Label>
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label></div>
    </form>

</body>
</html>

aspx.cs页面代码

protected void Button1_Click(object sender,EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
           
            //string clientpath = FileUpload1.PostedFile.FileName.ToString();           
            if(FileUpload1.PostedFile.ContentLength!=0)
             {               
                try
                {
                    string pathfirst = "C://Uploads//" + FileUpload1.FileName;
                    FileUpload1.SaveAs("C://Uploads//" +
                         FileUpload1.FileName);
                    Label1.Text = "File name: " +
                         FileUpload1.PostedFile.FileName + "<br>" +
                         FileUpload1.PostedFile.ContentLength + " kb<br>" +
                         "Content type: " +
                         FileUpload1.PostedFile.ContentType;
                    string sql = "insert into test(path) values('" + pathfirst + "')";
                    sqlConnection con = new sqlConnection("Data Source=192.168.1.69;Initial Catalog=calendar;Integrated Security=False;user ID=sa;Password=huison");
                    sqlCommand com = new sqlCommand(sql,con);
                    con.open();
                    com.ExecuteNonQuery();
                    con.Close();
                    Label2.Text = FileUpload1.FileName;

                }
                catch (Exception ex)
                {
                    Label1.Text = "ERROR: " + ex.Message.ToString();
                }
            }
            else
            {
                Label1.Text = "This file does not exist.";
            }
        }
        else
        {
            Response.Write("You have not specified a file.");

        }    }

 

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

相关推荐