HttpFileCollection myFileColl = Request.Files;
HttpPostedFile myFile = myFileColl.Get(0);//定义为你自己的路径
string FilePath = Request.PhysicalApplicationPath+"uploadfile\\";
string FileName = myFile.FileName.Substring(myFile.FileName.LastIndexOf("\\")+1);
if (!File.Exists(FilePath+FileName))
{
myFile.SaveAs(FilePath+FileName);
}
else
{
Response.Write = "文件已存在!";
}详细的解释可以看看帮助里的HttpPostedFile类
HttpPostedFile myFile = myFileColl.Get(0);//定义为你自己的路径
string FilePath = Request.PhysicalApplicationPath+"uploadfile\\";
string FileName = myFile.FileName.Substring(myFile.FileName.LastIndexOf("\\")+1);
if (!File.Exists(FilePath+FileName))
{
myFile.SaveAs(FilePath+FileName);
}
else
{
Response.Write = "文件已存在!";
}详细的解释可以看看帮助里的HttpPostedFile类
Directory.CreateDirectory(lstrFileFolder)
End If For i = 0 To Request.Files.Count - 1
allfiles = Request.Files(i) ' 获得文件名称
lstrFileName = allfiles.FileName
' 注:File.PostedFile.FileName 返回的是通过文件对话框选择的文件名(, 这之中包含了文件的目录信息)
lstrFileName = Path.GetFileName(lstrFileName)
' 去掉目录信息,返回文件名称 '上传文件到服务器
lstrFileNamePath = lstrFileFolder & lstrFileName
' 得到上传目录及文件名称
If lstrFileName <> "" Then
allfiles.SaveAs(lstrFileFolder + "\" + lstrFileName)
End If
Next
public virtual string FileUpLoad(string strURLPath) //ÉÏ´«Îļþº¯Êý
{
try
{
if (strTergertFile == "") //¼ì²éÔ´Îļþ·¾¶
{
throw new Exception("ÇëÉèÖÃstrOriFileÊôÐÔ¡£");
}
string strURL = strTergertFile; //½«Îļþ·¾¶´æ´¢
strFileState = "ÕýÔÚÑéÖ¤URLµÄÓÐЧÐÔ...";
int iNumber = strURL.LastIndexOf("\\");
string strAddress = strURL.Substring(0,iNumber); //È¡³öÎļþËùÔڵķ¾¶£¬ÍøÂçURLÎļþ¼ÐλÖÃ
string strFileName = strURL.Substring(iNumber+1,strURL.Length - (iNumber + 1)); //È¡µÃÏÂÔØÎļþÃû
string strURLFile = strURLPath + strFileName; //È¡µ½±¾µØµÄλÖúÍÎļþÃû
try //ÑéÖ¤URLµØÖ·
{
FilesOption fo = new FilesOption();
fo.strFilePath = strTergertFile;
System.Net.WebRequest myre = WebRequest.Create(strAddress); //´ò¿ªÒªÏÂÔصÄÎļþ¼Ð
strFileState = "Òѽ¨Á¢Á¬½Ó£¬¿ªÊ¼ÉÏ´«Îļþ...";
WebClient wcClient = new WebClient(); //´´½¨Ò»¸öÍøÒ³¿Í»§¶Ë
strFileState = "Òѽ¨Á¢Á¬½Ó£¬ÕýÔÚ·¢ËÍÊý¾Ý...";
wcClient.UploadFile(strURLFile,"POST",strTergertFile); //ÏÂÔØÎļþ
//ÁíÍâÒ»ÖÖ·½·¨
// Stream st = wcClient.OpenRead(strAddress); //½ÓÈëÊý¾ÝÁ÷
// StreamReader sr = new StreamReader(st); //¶Á³öÎļþÁ÷
// byte[] byFileText = new byte [100000];
// int iStart = 0; //ÉèÖÃÆðʼλ
// int iLimit = (int)byFileText.Length; //ÉèÖõ¥Ôª×î´ó½ÓÊÕ³¤¶È
// strFileState = "Òѽ¨Á¢Á¬½Ó£¬ÕýÔÚ½ÓÊÕÊý¾Ý...";
// while(iLimit > 0)
// {
// int iFlag = st.Read(byFileText,iStart,iLimit);
// if (iFlag == 0) //Èç¹ûÎļþ½ÓÊÕ½áÊø£¬Ìø³öÑ­»·
// {
// break;
// }
// iStart += iFlag;
// iLimit -= iFlag;
// }
// fo.FileWrite(byFileText,0,iLimit);
strFileState = "·¢ËÍÍê±Ï¡£";
}
catch(WebException exp)
{
throw new Exception(exp.Message);
}
return "";
}
catch(Exception ex)
{
return ex.Message;
}
}