網路上 runat=server 的範例太多,
今天學習的進度是,c#上傳檔案,不是透過runat="server" 的寫法.:
using System.IO;
public bool UploadFileCollection()
{
bool returnValue=false;
string uploadPath=GetFileUploadPath() + "\\";
try
{
//HTTP Context to get access to the submitted data
HttpContext postedContext = HttpContext.Current;
//File Collection that was submitted with posted data
HttpFileCollection Files = postedContext.Request.Files;
HttpFileCollection uploadFiles = Request.Files;
for (int i = 0; i < Files.Count; i++)
{
HttpPostedFile postedFile = Files[i];
// Access the uploaded file's content in-memory:
System.IO.Stream inStream = postedFile.InputStream;
byte[] fileData = new byte[postedFile.ContentLength];
inStream.Read(fileData, 0, postedFile.ContentLength);
// Save the posted file in our "data" virtual directory.
//postedFile.SaveAs(uploadPath + postedFile.FileName);
string filename;
filename = String.Format("{0}{1}", Guid.NewGuid().ToString("N"), Path.GetExtension(postedFile.FileName));
postedFile.SaveAs(uploadPath + filename);
// Also, get the file size and filename (as specified in
// the HTML form) for each file:
Response.Write("<li>" + postedFile.FileName + ": " + postedFile.ContentLength.ToString() + " bytes</li>");
returnValue=true;
}
}
catch (Exception ex1)
{
throw new Exception("Problem uploading file: " + ex1.Message);
}
return returnValue;
}
沒有留言:
張貼留言