asp.net 上传 不要fileupload控件

qflyj 2009-11-14 11:12:26
客户端用了fileupload控件(file控件)一样,
页面用的是ajax
但是在服务器端这个控件有失效了。

现在我想有没有什么方法,可以在服务器端
上传我指定的文件。

文件就是通过AJAX从客户端获得的。

httpPostFile

Request.File都没有效果。


能够用其他的办法没有哦


用代码控制上传。
...全文
283 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
xingdongfang 2009-11-15
  • 打赏
  • 举报
回复
swfupload
daichenghua 2009-11-15
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 lzsh0622 的回复:]
C# code// UpLoad.aspx<%@ Page language="c#" Codebehind="UpLoad.aspx.cs" AutoEventWireup="false" Inherits="WebPortal.Upload"%><!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD><t?-
[/Quote]


同意
webdiyer 2009-11-15
  • 打赏
  • 举报
回复
[Quote=引用楼主 qflyj 的回复:]
现在我想有没有什么方法,可以在服务器端
上传我指定的文件。
[/Quote]

不可以,除非用activex技术,否则能随便自动上传客户端文件的话,web就没有安全性可言了,某个人不小心访问一下你的某个页面,他电脑上公司的机密资料可能就被你给偷偷上传到你的服务器上去了,这以后谁还敢上网。

=============================
www.webdiyer.com
lzsh0622 2009-11-14
  • 打赏
  • 举报
回复

// UpLoad.aspx
<%@ Page language="c#" Codebehind="UpLoad.aspx.cs" AutoEventWireup="false" Inherits="WebPortal.Upload" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>多文件上传</title>
<script language="JavaScript">
function addFile()
{
var str = '<INPUT type="file" size="50" NAME="File">'
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
}
</script>
</HEAD>
<body>
<form id="form1" method="post" runat="server" enctype="multipart/form-data">
<div align="center">
<h3>多文件上传</h3>
<P id="MyFile"><INPUT type="file" size="50" NAME="File"></P>
<P>
<input type="button" value="增加(Add)" onclick="addFile()">
<input onclick="this.form.reset()" type="button" value="重置(ReSet)">
<asp:Button Runat="server" Text="开始上传" ID="UploadButton"></asp:Button>
</P>
<P>
<asp:Label id="strStatus" runat="server" Font-Names="宋体" Font-Bold="True" Font-Size="9pt"
Width="500px" BorderStyle="None" BorderColor="White"></asp:Label>
</P>
</div>
</form>
</body>
</HTML>

//UpLoad.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebPortal
{
/// <summary>
/// UpLoad 的摘要说明。
/// 实现多文件上传
/// </summary>
public class Upload : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button UploadButton;
protected System.Web.UI.WebControls.Label strStatus;

private void Page_Load(object sender, System.EventArgs e)
{
/// 在此处放置用户代码以初始化页面
if (this.IsPostBack) this.SaveImages();
}

private Boolean SaveImages()
{
///'遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;

/// '状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
strMsg.Append("上传的文件分别是:<hr color=red>");
try
{
for(int iFile = 0; iFile < files.Count; iFile++)
{
///'检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
fileExtension = System.IO.Path.GetExtension(fileName);
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
strMsg.Append("上传文件的文件名:" + fileName + "<br>");
strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>");
///'可根据扩展名字的不同保存到不同的文件夹
///注意:可能要修改你的文件夹的匿名写入权限。
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("images/") + fileName);
}
}
strStatus.Text = strMsg.ToString();
return true;
}
catch(System.Exception Ex)
{
strStatus.Text = Ex.Message;
return false;
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.ID = "Upload";
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}
qflyj 2009-11-14
  • 打赏
  • 举报
回复
FileStream nFile = File.Open(filePath, FileMode.Create, FileAccess.Write);
StreamWriter writer = new StreamWriter(nFile);

FileStream sourceFile = File.Open(myfileName, FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(sourceFile);

string strLine = reader.ReadLine();
while (strLine != null)
{
writer.Write(strLine);
}

reader.Close();
sourceFile.Close();
writer.Close();
nFile.Close();

这样写我都不能够上传
显示上传文件在变大
wuyq11 2009-11-14
  • 打赏
  • 举报
回复
红街咖啡 2009-11-14
  • 打赏
  • 举报
回复


/// <summary>
/// ConvertStreamToByteBuffer:把给定的文件流转换为二进制字节数组。
/// </summary>
/// <param name="theStream"></param>
/// <returns></returns>
public byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
{
int b1;
System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
while ((b1 = theStream.ReadByte()) != -1)
{
tempStream.WriteByte(((byte)b1));
}
return tempStream.ToArray();
}
红街咖啡 2009-11-14
  • 打赏
  • 举报
回复
lz写个Web Service

[code=c#]
private static ReturnValue loadUserFile(byte[] fs, string filePath)
{
string Message=string.Empty;
if (!File.Exists(filePath))
{
try
{
/**/
///定义并实例化一个内存流,以存放提交上来的字节数组。
System.IO.MemoryStream m = new System.IO.MemoryStream(fs);
//取出存放地址,可以通过数据库里存放,lz不用定死了。此处只是做DEMO。
string strFile = filePath;
/**/
///定义实际文件对象,保存上载的文件。
System.IO.FileStream fl = new System.IO.FileStream(strFile, FileMode.OpenOrCreate);
/**/
///把内内存里的数据写入物理文件
m.WriteTo(fl);
m.Close();
fl.Close();
m = null;
fl = null;
ret.HasError = false;
ret.Message = "上传文件成功";
string logMessge = string.Format("WebCards_FileManager上传了文件{0}",filePath);

return ture
//return false;
}
catch (Exception ex)
{
Logger.WriteLog(ex);
message = "创建文件失败"; //返回消息
return false



}
}
else
{
message = "此文件已经存在,请更换文件名或上传其他文件";
return false;
}
}
[/code]
小_虎 2009-11-14
  • 打赏
  • 举报
回复
HttpPostedFile
happy664618843 2009-11-14
  • 打赏
  • 举报
回复
问题太高深了 不好搞 sf友情帮顶

62,254

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧