请教一个文件上传的问题!

天狼武士 2009-01-12 10:45:22
我想根据客户端提交的地址上传文件请问该怎么做?比如客户端有个地址是:"F:\1.txt"这个文件,我点上传后根据这个地址读取客户端1.txt这个文件然后上传到我服务端指定文件夹里面!请问该如何写?谢谢!!!
...全文
161 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
love531000 2009-01-13
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20090113/12/66feb712-e95e-46a2-b8cc-0105218830e7.html
天狼武士 2009-01-13
  • 打赏
  • 举报
回复
你那个File和Files是啥?
天狼武士 2009-01-13
  • 打赏
  • 举报
回复
你那个File和Files是啥?
天狼武士 2009-01-12
  • 打赏
  • 举报
回复
服务器端我是存放在Server.MapPath("UploadFile")底下的啊!
lambober 2009-01-12
  • 打赏
  • 举报
回复
客户端的地址是什么,存到服务器上的地址就是什么?
感觉好复杂啊。而且,万一服务器尚没有那个盘符呢
天狼武士 2009-01-12
  • 打赏
  • 举报
回复
恩!我就是想做成多文件上传的!可是增加一个加一个<input>这样不太美观啊!所以就想放到ListBox里面!但发现取不了数据!
cookies10wen 2009-01-12
  • 打赏
  • 举报
回复

if ((FileUpload1.HasFile) == true) {
///'''上传文件''''''''''''''
string savepath = Server.MapPath("Data\\");
string filename = null;
string filekjm = null;
string ip = null;
ip = Request.ServerVariables("REMOTE_ADDR");
ip = Strings.Replace(ip, ".", "");
filekjm = System.IO.Path.GetExtension(FileUpload1.FileName);
if (filekjm != ".xls") {
Response.Write("<script>alert('请导入Excel文件!!!')</script>");
return;
}
filename = DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ip + filekjm;
filename = Strings.Replace(filename, " ", "");
savepath += filename;
FileUpload1.SaveAs(savepath);
}

lovehongyun 2009-01-12
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 skywolfkun 的回复:]
额~关键现在是这样的!我先的到它在客户端的地址,“F:\1.txt"然后点增加,增加到ListBox里面,然后再输入地址“F:\2.xls”然后再点添加添加到ListBox里面,然后点上传,根据ListBox里面的ITEM的地址然后上传啊!
[/Quote]

你可以做成多文件上传,动态添加<input type="file">
然后只把文件路径放到Listbox中即可.
后台用Request.Files获取.然后SaveAs
天狼武士 2009-01-12
  • 打赏
  • 举报
回复
额~关键现在是这样的!我先的到它在客户端的地址,“F:\1.txt"然后点增加,增加到ListBox里面,然后再输入地址“F:\2.xls”然后再点添加添加到ListBox里面,然后点上传,根据ListBox里面的ITEM的地址然后上传啊!
wangzhenyue 2009-01-12
  • 打赏
  • 举报
回复
当然可以了。。



using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _uploadIT : System.Web.UI.Page
{
//protected System.Web.UI.WebControls.Button UploadButton;
// protected System.Web.UI.WebControls.Label strStatus;

protected void Page_Load(object sender, EventArgs e)
{
if (Session["adminName"].ToString().Trim() != "")
{
if((Convert.ToInt32(Session["level"].ToString()) == 3 && Convert.ToInt32(Session["class"].ToString()) == 1)|| (Convert.ToInt32(Session["level"].ToString()) == 5))
//(Convert.ToInt32(Session["level"].ToString()) >= 4)
{

}
else
{
Response.Redirect("error.html");
}

}
else
{
Response.Redirect("login.aspx");
}


}

private Boolean SaveImages()
{

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, file_id;


int year = System.DateTime.Now.Year;
int month = System.DateTime.Now.Month;
int day = System.DateTime.Now.Day;
int hour = System.DateTime.Now.Hour;
int minute = System.DateTime.Now.Minute;
int second = System.DateTime.Now.Second;
int millisecond = System.DateTime.Now.Millisecond;
//string my_file_id = year.ToString() + month.ToString() + day.ToString() + hour.ToString() + minute.ToString() + second.ToString() + millisecond.ToString() + iFile.ToString();

fileName = System.IO.Path.GetFileName(postedFile.FileName);
fileExtension = System.IO.Path.GetExtension(fileName);
file_id = fileName;
if (fileName != "")
{
fileExtension = System.IO.Path.GetExtension(fileName);
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
strMsg.Append("上传文件的文件名:" + file_id + "<br>");
strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>");
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("upload/") + file_id);
}
}
strStatus.Text = strMsg.ToString();
return true;
}
catch (System.Exception Ex)
{
strStatus.Text = Ex.Message;
return false;
}
}

protected void UploadButton_Click(object sender, EventArgs e)
{
Boolean bl = SaveImages();
if (bl)
{
adminDataBase db = new adminDataBase();
int l = db.Log(Session["adminName"].ToString(), "上传文件");
Response.Write("<script>alert('上传成功!')</script>");
}
else
{
Response.Write("<script>alert('上传失败!')</script>");
}
}
}


biaofanwo 2009-01-12
  • 打赏
  • 举报
回复
都说差不多了
Liu_Qiang 2009-01-12
  • 打赏
  • 举报
回复
string fileType = this.upSheetFile.PostedFile.ContentType;
if (fileType != "text/xml")
{
this.lblErr.Text = "上传文件类型不正确!";
return;
}

if (this.upSheetFile.PostedFile.ContentLength == 0)
{
this.lblErr.Text = "请选择要上传的试卷";
}
else
{
string sheetName = upSheetFile.PostedFile.FileName;
XmlDocument xd = new XmlDocument();
try
{
xd.Load(upSheetFile.PostedFile.InputStream);
string title = xd.SelectSingleNode("//title").InnerText;
string score = xd.SelectSingleNode("//score").InnerText;
string timing = xd.SelectSingleNode("//timing").InnerText;
string tmcount = xd.SelectSingleNode("//tmcount").InnerText;
string content = xd.SelectSingleNode("//content").InnerText.Replace("\t", "").Replace("\r", "").Replace("\n", "");//.Replace(" ","カ")
content = createimg(content);

if (content == "")
{
this.lblErr.Text = "试卷上传失败,操作无写入权限!";
return;
}
string jsonTiMuScore = xd.SelectSingleNode("//JsonTiMuScore").InnerText;
string jsonAnswer = xd.SelectSingleNode("//JsonAnswer").InnerText;
string GUID = xd.SelectSingleNode("//GUID").InnerText;
string sheetID = new DBSheet().InsertSheetFile(title, score, timing, tmcount, content, jsonTiMuScore, jsonAnswer, TeacherID, GUID);
if (sheetID == "")
{
this.lblErr.Text = "试卷上传失败或试卷已存在!";
}
else
{
Public.CreateTestFile(sheetID, content, Request.PhysicalApplicationPath);
this.lblErr.Text = "试卷上传成功!" + sheetID;
Response.Redirect("SheetEdit.aspx", true);
//Response.Write("<script>window.location.reload()</script>");
//BindSheet();
}

}
catch (Exception ex)
{
this.lblErr.Text = "上传文件格式不正确!" + ex.Message;
}


试着改一下
lovehongyun 2009-01-12
  • 打赏
  • 举报
回复
不用这种方法你是没权限来操作客户端文件的.
activex除外
sprc_lcl 2009-01-12
  • 打赏
  • 举报
回复
/// <summary>
/// 上传单个文件
/// </summary>
/// <param name="File1">文件控件</param>
/// <param name="sizes">上传大小限制,KB单位;为小于等于0不限</param>
/// <param name="filetype">文件后缀限定,以“|”隔开,不带"."号。为空不限</param>
/// <param name="filepath">上传到文件夹路径,为空为当前文件夹</param>
/// <param name="returnmessage">上传返回信息</param>
/// <returns>返回上传的文件名虚拟路径</returns>
public static string UploadFile(System.Web.UI.HtmlControls.HtmlInputFile File1, int sizes, string filetype, string filepath,ref string returnmessage)
{
returnmessage = "";
try
{
filepath = filepath.Trim();
if (filepath.LastIndexOf("/") != filepath.Length - 1)
{
filepath += "/";
}
string path = filepath; //保存至的文件夹
if (path == "")
{
path = "./";
}
path = System.Web.HttpContext.Current.Server.MapPath(path);

//判断是否有上传的文件
string sourcepath = File1.Value.Trim();//文件的上传的路径
if (sourcepath == "" || sourcepath == null)
{
returnmessage = "无上传文件";
return "";
}

string tfiletype = sourcepath.Substring(sourcepath.LastIndexOf(".") + 1).ToLower();
//如果要求的后缀不为空则判断上传的文件的后缀是否合适
if (filetype != "")
{
if ((filetype.ToLower() + "|").IndexOf(tfiletype + "|") < 0)
{
returnmessage = "上传的文件类型不符合设定";
return "";
}
}

if (sizes > 0)
{
int sizes2 = sizes * 1024;
//获得上传文件的大小
long strLen = File1.PostedFile.ContentLength;
//判断上传文件大小
if (strLen > sizes2)
{
returnmessage = "上传的文件不能大于" + sizes.ToString() + "KB";
return "";
}
}

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
//判断文件夹否存在,不存在则创建
if (!dir.Exists)
{
dir.Create();
}

string modifyfilename = Tools.GetId(0,12);
path = path + modifyfilename + "." + tfiletype;
while (System.IO.File.Exists(path))
{
modifyfilename = Tools.GetId(0, 12);
path = path + modifyfilename + "." + tfiletype;
}
File1.PostedFile.SaveAs(path);
filepath = filepath + modifyfilename + "." + tfiletype;
returnmessage = "上传成功";
return filepath;
}
catch
{
returnmessage = "上传出现错误";
return "";
}
}
lovehongyun 2009-01-12
  • 打赏
  • 举报
回复
用<input type="file">
或asp.net中的fileupload控件直接上传.

zhxhdean 2009-01-12
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20071010/15/679adbb7-a767-467b-892a-03c582a81b46.html
greystar 2009-01-12
  • 打赏
  • 举报
回复
只要你有相应的权限,你就能将文件另存为在服务器上的某个目录里.上传控件不需要你自己去读文件的.自己会上什相应的数据
Robin 2009-01-12
  • 打赏
  • 举报
回复
太详细了,我就不多说了
阿彪兄 2009-01-12
  • 打赏
  • 举报
回复
楼上已经说得很清楚,支持一下
SilverNet 2009-01-12
  • 打赏
  • 举报
回复
我做了个上传整个文件夹的程序可以给你参考下

/// <summary>
/// 上传当前文件夹下面的所有文件
/// </summary>

private void UploadCurrentFolder()
{
//获取文件夹的路径
int filenameLastindex = filename.LastIndexOf(@"\");
string dirinfo = filename.Substring(0, filenameLastindex); //文件夹的路径
DirectoryInfo dir = new DirectoryInfo(dirinfo);


//获取文件夹的名称
int dirinfoLastindex = dirinfo.LastIndexOf(@"\");
string dirname = dirinfo.Substring(0, dirinfoLastindex);
dirname = dirinfo.Replace(dirname, ""); //文件夹名称

//上传到服务器的路径
string path = Server.MapPath(@"~/upload/" + dirname + "/");



//将文件夹里的所有数据传输到服务器上
string newFilename;
string astractFileType = ".jpg.gif.exe";
foreach (FileInfo files in dir.GetFiles())
{
//获取上传文件的类型
string FileExtension = files.Extension;
//需要限制上传文件的类型
if (astractFileType.IndexOf(FileExtension) >= 0)
{
Response.Redirect("Upload.aspx?stateinfo=提示:文件夹中含有JPG,GIF,EXE类型格式的文件!不是允许的格式。部分文件没有被上传");
}
else if (files.Length > 1024000 * 150)
{
Response.Redirect("Upload.aspx?stateinfo=提示:上传的文件超过允许的上限!最大上传文件限制150M!");
}

else
{
//如果服务器上该文件夹不存在的话,创建文件夹
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
//如果文件存在就改变文件名
if (File.Exists(path + files.Name))
{
//加上日期,精确到毫秒
newFilename = DateTime.Now.ToString("yyyyMMddHHmmssff") + files.Name;
}
//如果文件名超长
else if (files.Name.Length > 50)
{
newFilename = DateTime.Now.ToString("yyyyMMddHHmmssff") + files.Name.Substring(0, 47) + "……" + files.Extension;
}
else
{
newFilename = files.Name;
}

files.CopyTo(path + newFilename);
Response.Redirect("Upload.aspx?stateinfo=上传成功!");
}


}

加载更多回复(5)

62,269

社区成员

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

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

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

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