Notice (8): file_put_contents(): Write of 270 bytes failed with errno=28 No space left on device [CORE/src/Log/Engine/FileLog.php, line 140]

Notice: file_put_contents() [function.file-put-contents]: Write of 1108 bytes failed with errno=28 No space left on device in /www/wwwroot/www.adminso.com/vendor/cakephp/cakephp/src/Log/Engine/FileLog.php on line 140
Notice (8): SplFileObject::fwrite() [<a href='https://secure.php.net/splfileobject.fwrite'>splfileobject.fwrite</a>]: Write of 5131 bytes failed with errno=28 No space left on device [CORE/src/Cache/Engine/FileEngine.php, line 141]

Notice: file_put_contents() [function.file-put-contents]: Write of 3152 bytes failed with errno=28 No space left on device in /www/wwwroot/www.adminso.com/vendor/cakephp/cakephp/src/Log/Engine/FileLog.php on line 140
Notice (8): SplFileObject::fwrite() [<a href='https://secure.php.net/splfileobject.fwrite'>splfileobject.fwrite</a>]: Write of 5437 bytes failed with errno=28 No space left on device [CORE/src/Cache/Engine/FileEngine.php, line 141]

Notice: file_put_contents() [function.file-put-contents]: Write of 3152 bytes failed with errno=28 No space left on device in /www/wwwroot/www.adminso.com/vendor/cakephp/cakephp/src/Log/Engine/FileLog.php on line 140
Notice (8): SplFileObject::fwrite() [<a href='https://secure.php.net/splfileobject.fwrite'>splfileobject.fwrite</a>]: Write of 133 bytes failed with errno=28 No space left on device [CORE/src/Cache/Engine/FileEngine.php, line 141]

Notice: file_put_contents() [function.file-put-contents]: Write of 2791 bytes failed with errno=28 No space left on device in /www/wwwroot/www.adminso.com/vendor/cakephp/cakephp/src/Log/Engine/FileLog.php on line 140
Warning (512): long cache was unable to write 'a26ef5f1aeb9dc54c21e41b64146a83d' to Cake\Cache\Engine\FileEngine cache [CORE/src/Cache/Cache.php, line 275]

Notice: file_put_contents() [function.file-put-contents]: Write of 2586 bytes failed with errno=28 No space left on device in /www/wwwroot/www.adminso.com/vendor/cakephp/cakephp/src/Log/Engine/FileLog.php on line 140
Notice (8): SplFileObject::fwrite() [<a href='https://secure.php.net/splfileobject.fwrite'>splfileobject.fwrite</a>]: Write of 130 bytes failed with errno=28 No space left on device [CORE/src/Cache/Engine/FileEngine.php, line 141]

Notice: file_put_contents() [function.file-put-contents]: Write of 2791 bytes failed with errno=28 No space left on device in /www/wwwroot/www.adminso.com/vendor/cakephp/cakephp/src/Log/Engine/FileLog.php on line 140
Warning (512): long cache was unable to write '78cf051f9a27fa27ad6c5e75af2d6a2e' to Cake\Cache\Engine\FileEngine cache [CORE/src/Cache/Cache.php, line 275]

Notice: file_put_contents() [function.file-put-contents]: Write of 2586 bytes failed with errno=28 No space left on device in /www/wwwroot/www.adminso.com/vendor/cakephp/cakephp/src/Log/Engine/FileLog.php on line 140
Notice (8): SplFileObject::fwrite() [<a href='https://secure.php.net/splfileobject.fwrite'>splfileobject.fwrite</a>]: Write of 28309 bytes failed with errno=28 No space left on device [CORE/src/Cache/Engine/FileEngine.php, line 141]

Notice: file_put_contents() [function.file-put-contents]: Write of 2793 bytes failed with errno=28 No space left on device in /www/wwwroot/www.adminso.com/vendor/cakephp/cakephp/src/Log/Engine/FileLog.php on line 140
Warning (512): 1hours cache was unable to write 'b8236d1d820596d29f72bd9108102045' to Cake\Cache\Engine\FileEngine cache [CORE/src/Cache/Cache.php, line 275]

Notice: file_put_contents() [function.file-put-contents]: Write of 2588 bytes failed with errno=28 No space left on device in /www/wwwroot/www.adminso.com/vendor/cakephp/cakephp/src/Log/Engine/FileLog.php on line 140
Notice (8): unserialize() [<a href='https://secure.php.net/function.unserialize'>function.unserialize</a>]: Error at offset 4076 of 4085 bytes [APP/Controller/NewsController.php, line 5571]

Notice: file_put_contents() [function.file-put-contents]: Write of 2488 bytes failed with errno=28 No space left on device in /www/wwwroot/www.adminso.com/vendor/cakephp/cakephp/src/Log/Engine/FileLog.php on line 140
Notice (8): unserialize() [<a href='https://secure.php.net/function.unserialize'>function.unserialize</a>]: Error at offset 4076 of 4085 bytes [APP/Controller/NewsController.php, line 5571]

Notice: file_put_contents() [function.file-put-contents]: Write of 2488 bytes failed with errno=28 No space left on device in /www/wwwroot/www.adminso.com/vendor/cakephp/cakephp/src/Log/Engine/FileLog.php on line 140
C#实现Web文件上传的两种好方法 - 站长搜索
首页 > 资讯列表 > 软件学院 >> 系统程序

C#实现Web文件上传的两种好方法

系统程序 2014-12-07 21:15:04 转载来源: 网络整理/侵权必删

  1. C#实现Web文件的上传   在Web编程中,我们常需要把一些本地文件上传到Web服务器上,上传后,用户可以通过浏览器方便地浏览这些文件,应用十分广泛。   那么使用C#如何实现文件上传的功能呢?下面笔者简要介绍一下

  

1. C#实现Web文件上传

  在Web编程中,我们常需要把一些本地文件上传到Web服务器上,上传后,用户可以通过浏览器方便地浏览这些文件,应用十分广泛。

  那么使用C#如何实现文件上传的功能呢?下面笔者简要介绍一下。

  首先,在你的Visual C# web project 中增加一个上传用的Web Form,为了要上传文件,需要在ToolBox中选择HTML类的File Field控件,将此控件加入到Web Form中,然而此时该控件还不是服务端控件,我们需要为它加上如下一段代码:<input id=uploadfile1 type=file size=49 runat="server">,这样它就成为服务端控件了,如果需要同时上传数个文件时,我们可以相应增加此控件。

  需要注意的是代码中一定要把<form>的属性设置成为:

<form method=post encType=multipart/ form-data runat="server">

  如果没有这个属性,就不能实现上传。

  然后在此Web Form中增加一个Web Form类的Button,双击Button添加如下代码:

//上传图片的程序段 DateTime now = DateTime.Now ; //取现在时间到DataTime类的对象now中 string strBaseLocation = "D:\web\FC\pic\"; //这是文件将上传到的服务器的绝对目录 if (uploadfile1.PostedFile.ContentLength != 0) //判断选取对话框选取的文件长度是否为0 {     uploadfile1.PostedFile.SaveAs(strBaseLocation+now.DayOfYear.ToString()+uploadfile1.PostedFile.ContentLength.ToString()+".jpg"); //执行上传,并自动根据日期和文件大小不同为文件命名,确保不重复 Label1.Text="图片1已经上传,文件名为:"+now.DayOfYear.ToString()+uploadfile1.PostedFile.ContentLength.ToString()+".jpg";  navigator.Insert(System.Xml.TreePosition.After, XmlNodeType.Element,"pic1","","") ;     navigator.Insert(System.Xml.TreePosition.FirstChild, XmlNodeType.Text,"pic1","","") ;     navigator.Value= now.DayOfYear.ToString()+uploadfile1.PostedFile.ContentLength.ToString()+".jpg" ;     navigator.MoveToParent() ; }

  上面的代码用于笔者开发的一个使用XML文件存储新闻信息的系统中,后面几句代码作用是写上传文件信息到XML文件中。如果要上传其他类型文件,只需要将jpg改为相应类型的后缀名即可,如改为doc即可上传Word文件,浏览器即可直接浏览上传的Word文件。

  【注意事项】

  1. 上传文件不可以无限大;

  2. 要注意IIS的安全性方面的配合;

  3. 用Visual Studio 的安装项目做安装程序的时候,请注意安装程序所在的绝对路径问题;

  4. 注意文件上传后的重名问题。

2. C#实现Web文件的上传

using System; using System.Data; using System.Data.SqlClient; using System.Web.UI.HtmlControls; using System.Drawing.Imaging; using System.Configuration; using System.Drawing; namespace zhuanti { /// <summary> /// 这是一个用于玩家投稿中实现玩家上传文件功能中用到的相应的函数的功能模块 /// </summary> public class FileUpload { public enum File //定义一个人用于存放玩家上传文件信息的一个数组 { FILE_SIZE , //大小 FILE_POSTNAME, //类型(文件后缀名) FILE_SYSNAME , //系统名 FILE_ORGINNAME, //原来的名字 FILE_PATH //文件路径 } private static Random rnd = new Random(); //获取一个随机数 public static string[] UploadFile(HtmlInputFile file,string Upload_Dir) //实现玩家文件上传功能的主函数 { string[] arr = new String[5]; string FileName = GetUniquelyString(); //获取一个不重复的文件名 string FileOrginName = file.PostedFile.FileName.Substring (file.PostedFile.FileName.LastIndexOf("\")+1);//获取文件的原始名 if(file.PostedFile.ContentLength<=0) { return null; } string postFileName; string FilePath = Upload_Dir.ToString(); string path = FilePath + "\"; try { int pos = file.PostedFile.FileName.LastIndexOf(".")+1; postFileName = file.PostedFile.FileName.Substring(pos,file.PostedFile.FileName.Length-pos); file.PostedFile.SaveAs(path+FileName+"."+postFileName); //存储指定的文件到指定的目录 } catch(Exception exec) { throw(exec); } double unit = 1024; double size = Math.Round(file.PostedFile.ContentLength/unit,2); arr[(int)File.FILE_SIZE] = size.ToString(); //文件大小 arr[(int)File.FILE_POSTNAME] = postFileName; //文件类型(文件后缀名) arr[(int)File.FILE_SYSNAME] = FileName; //文件系统名 arr[(int)File.FILE_ORGINNAME] = FileOrginName; //文件原来的名字 arr[(int)File.FILE_PATH]=path+FileName+"."+postFileName; //文件路径 return arr; } public static bool OperateDB(string sqlstr) //建立一个和数据库的关联 { if (sqlstr==String.Empty) return false; SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connstring"]); SqlCommand myCommand = new SqlCommand(sqlstr, myConnection); myConnection.Open(); myCommand.ExecuteNonQuery(); myConnection.Close(); return true; } public static string GetUniquelyString() //获取一个不重复的文件名 { const int RANDOM_MAX_VALUE = 1000; string strTemp,strYear,strMonth,strDay,strHour,strMinute,strSecond,strMillisecond; DateTime dt =DateTime.Now; int rndNumber = rnd.Next(RANDOM_MAX_VALUE); strYear = dt.Year.ToString (); strMonth = (dt.Month > 9)? dt.Month.ToString() : "0" + dt.Month.ToString(); strDay = (dt.Day > 9)? dt.Day.ToString() : "0" + dt.Day.ToString(); strHour = (dt.Hour > 9)? dt.Hour.ToString() : "0" + dt.Hour.ToString(); strMinute = (dt.Minute > 9)? dt.Minute.ToString() : "0" + dt.Minute.ToString(); strSecond = (dt.Second > 9)? dt.Second.ToString() : "0" + dt.Second.ToString(); strMillisecond = dt.Millisecond.ToString(); strTemp = strYear + strMonth + strDay +"_"+ strHour + strMinute + strSecond +"_"+ strMillisecond +"_"+ rndNumber.ToString () ; return strTemp; } } } 

(责任编辑:admin)

标签: 实现 Web 文件 上传 两种 方法


声明:本文内容来源自网络,文字、图片等素材版权属于原作者,平台转载素材出于传递更多信息,文章内容仅供参考与学习,切勿作为商业目的使用。如果侵害了您的合法权益,请您及时与我们联系,我们会在第一时间进行处理!我们尊重版权,也致力于保护版权,站搜网感谢您的分享!

站长搜索

http://www.adminso.com

Copyright @ 2007~2025 All Rights Reserved.

Powered By 站长搜索

打开手机扫描上面的二维码打开手机版


使用手机软件扫描微信二维码

关注我们可获取更多热点资讯

站长搜索目录系统技术支持