kk.PostedFile.InputStream()
用一个流直接读取,可以是内存流也可以是文件流,然后保存到一个字节数组中!
将字节数组做为参数传到command
用一个流直接读取,可以是内存流也可以是文件流,然后保存到一个字节数组中!
将字节数组做为参数传到command
解决方案 »
- vs2008在windows 2003环境下开发的系统在windows 2008环境运行出现的问题
- asp.net通用查询
- 网络教学课程系统
- GridView中怎么获取HyperlinkField和TemplateField的列号?
- 问个大小写问题,期待高手帮忙。
- asp.net2.0中的符号 ~ 是什么意思!各位大虾谁知道啊!
- 请教一个简单的问题
- 具有挑战性的技术问题:如何用ASP.NET 来动态生成一个POWERPOINT 文件?急!
- 自定义控件中怎样使用AJAX,在线急等,谢谢
- 水晶报表(高分回报)
- 请教vs下建立的web-aspx怎么移到其他机器上就不能运行了??
- 两个小问题图片的!!!!!!!!!!!!!!
if (File1.PostedFile!=null)
{
try
{
//int Len=0;
//string FileName="";
Image1.ImageUrl=File1.Value;
//Len=File1.Value.LastIndexOf(@"\");
//FileName=File1.Value.Substring(Len + 1);
// 文件另存
//File1.PostedFile.SaveAs(@"c:\temp\"+FileName);
// }
if(files[0].ContentLength>400)
{
message.InnerHtml="上传的图片大小限定在100K以内!";
Page.RegisterStartupScript("","<script>alert('"+"注意:上传的图片大小限定在100K以内!"+"');</script>");
errorno=1;
}
HttpPostedFile myFile = UploadFile.PostedFile;
//FileName.Text = myFile.FileName;
if(myFile.ContentLength > 102400)
{
Response.Write("<script language=javascript>");
Response.Write("alert('上传文件太大,系统限制最大为100K!');");
Response.Write("</script>");
return;
}
基于http流上载:
---------------------
private void cmdSendFileData_Click(object sender, System.EventArgs e)
{
// This method takes a local file named datafile.txt and sends its
// contents via a WebRequest to a website. The contents are sent via
// HTTP using the Request stream of the WebRequest object. The file
// is accessed using a FileStream.
string strMsg = "In order to run this part of the sample, you must adjust the security settings" +
" for the physical directory that contains the ASPX files." + Environment.NewLine + Environment.NewLine +
"Please see the Readthis.htm file for more information." + Environment.NewLine +
"Also see the source code (for the procedure cmdSendFileData_Click) to remove this warning after the security settings have been adjusted."; FileStream fs = null; //' To access the local file;
WebRequest req = null; //' Reference to the Webrequest;
// Wrap the stream access in a try {/Finally block to guarantee a timely
// release of the stream resources. try
{
// Access the file
fs = new FileStream("..//..//DataFile.txt", FileMode.Open);
// Create the WebRequest instance
req = WebRequest.Create("http://localhost/SendAndReceiveDataWebPages/SendData.aspx");
// Use POST since we're sending content in the body.
req.Method = "POST";
// Copy from the file into the RequestStream
CopyData(fs, req.GetRequestStream());
}
catch( Exception exp)
{
MessageBox.Show(exp.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
finally
{
try
{
// Guarantee the streams will be closed
if (req != null) req.GetRequestStream().Close();
if (fs != null) fs.Close();
}
catch
{
// Eat the error if we get one
}
} WebResponse rsp = null; try
{
// This actually sends the data to the Web Server
rsp = req.GetResponse();
if (Convert.ToDouble(rsp.Headers["Content-Length"]) == 0)
{
MessageBox.Show("Data Sent Sucessfully.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch( Exception exp)
{
MessageBox.Show(exp.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
try
{
if ( rsp != null) rsp.Close();
}
catch{
}
}
}
---------------------
基于http流下载:
---------------------private void cmdReceiveDataFile_Click(object sender, System.EventArgs e)
{
// This method requests and receives an XML file from a website.
// The file is streamed back to this method, which copies the contents
// into a local file named ReceivedXMLFile.xml. The contents are streamed
// using the ResponseStream of the WebResponse class. The stream access
// is wrapped in try {/catch blocks to ensure timely release of the
// resources.
string strMsg = "In order to run this part of the sample, you must adjust the security settings" +
" for the physical directory that contains the ASPX files." + Environment.NewLine + Environment.NewLine +
"Please see the Readthis.htm file for more information." + Environment.NewLine +
"Also see the source code (for the procedure cmdReceiveImageFile_Click) to remove this warning after the security settings have been adjusted.";
FileStream fs = null; //' To access the local file;
WebRequest req;
StreamReader sr; try
{
// This sets up the Request instance
req = WebRequest.Create("http://localhost/SendAndReceiveDataWebPages/ReceiveData.aspx");
// Use a GET since no data is being sent to the web server
req.Method = "GET";
// This causes the round-trip
WebResponse rsp = req.GetResponse();
try
{
// Open the file to stream in the content
fs = new FileStream("ReceivedFile.rar", FileMode.Create);
// Copy the content from the response stream to the file.
CopyData(rsp.GetResponseStream(), fs);
}
catch( Exception exp)
{
// Will catch any error that we're not explicitly trapping.
MessageBox.Show(exp.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
finally
{
// Guarantee the streams will be closed
if (rsp != null) rsp.GetResponseStream().Close();
if (fs != null) fs.Close();
}
MessageBox.Show("Receive of data file completed successfully!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch( Exception exp)
{
// Will catch any error that we're not explicitly trapping.
MessageBox.Show(exp.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
}--------------------------
方法:
--------------------------
private void CopyData(Stream FromStream, Stream ToStream)
{
// This routine copies content from one stream to another, regardless
// of the media represented by the stream.
// This will track the # bytes read from the FromStream
int intBytesRead;
// The maximum size of each read
const int intSize = 4096;
Byte[] bytes = new Byte[intSize];
// Read the first bit of content, then write and read all the content
// From the FromStream to the ToStream.
intBytesRead = FromStream.Read(bytes, 0, intSize); while (intBytesRead > 0)
{
ToStream.Write(bytes, 0, intBytesRead);
intBytesRead = FromStream.Read(bytes, 0, intSize);
}
}
namespace Wmj
{
public class MyUpload
{
private System.Web.HttpPostedFile postedFile=null;
private string savePath="";
private string extension="";
private int fileLength=0;
//显示该组件使用的参数信息
public string Help
{
get{
string helpstring;
helpstring="<font size=3>MyUpload myUpload=new MyUpload(); //构造函数";
helpstring+="myUpload.PostedFile=file1.PostedFile;//设置要上传的文件";
helpstring+="myUpload.SavePath=\"e:\\\";//设置要上传到服务器的路径,默认c:\\";
helpstring+="myUpload.FileLength=100; //设置上传文件的最大长度,单位k,默认1k";
helpstring+="myUpload.Extension=\"doc\";设置上传文件的扩展名,默认txt";
helpstring+="label1.Text=myUpload.Upload();//开始上传,并显示上传结果</font>";
helpstring+="<font size=3 color=red>Design By WengMingJun 2001-12-12 All Right Reserved!</font>";
return helpstring;
}
}public System.Web.HttpPostedFile PostedFile
{
get
{
return postedFile;
}
set
{
postedFile=value;
}
}public string SavePath
{
get
{
if(savePath!="") return savePath;
return "c:\\";
}
set
{
savePath=value;
}
}public int FileLength
{
get
{
if(fileLength!=0) return fileLength;
return 1024;
}
set
{
fileLength=value*1024;
}
}public string Extension
{
get
{
if(extension!="") return extension;
return "txt";
}
set
{
extension=value;
}
}public string PathToName(string path)
{
int pos=path.LastIndexOf("\\");
return path.Substring(pos+1);
}public string Upload()
{
if(PostedFile!=null)
{
try{
string fileName=PathToName(PostedFile.FileName);
if(!fileName.EndsWith(Extension)) return "You must select "+Extension+" file!";
if(PostedFile.ContentLength>FileLength) return "File too big!";
PostedFile.SaveAs(SavePath+fileName);
return "Upload File Successfully!";
}
catch(System.Exception exc)
{return exc.Message;}
}
return "Please select a file to upload!";
}
}
}用csc /target:Library Wmj.cs 编译成dll供以后多次调用
调用举例
<%@page language="C#" runat="server"%>
<%@import namespace="Wmj"%>
<script language="C#" runat="server">
void Upload(object sender,EventArgs e)
{
MyUpload myUpload=new MyUpload();
// label1.Text=myUpload.Help;
myUpload.PostedFile=file1.PostedFile;
myUpload.SavePath="e:\\";
myUpload.FileLength=100;
label1.Text=myUpload.Upload();
}
</script>
<form enctype="multipart/form-data" runat="server">
<input type="file" id="file1" runat="server"/>
<asp:Button id="button1" Text="Upload" OnClick="Upload" runat="server"/>
<asp:Label id="label1" runat="server"/>
</form>
using System.Net; //' Used for client-side of HTTP communication;
你自己打开msdn帮助看看Stream类,是在那个名字空间下的,然后把它引入就行了。
我觉得是io,要不就是text
十分感谢各位好汉。
郁闷中:
感谢各位好汉