0
我有一個下拉列表&文件上傳器。在我需要的時候,下拉列表選擇&之後需要加載文件上傳器。在我的編碼中總是顯示沒有文件。 在這裏,我需要使用文件上傳器將SelectedValue傳遞到數據庫。選擇下拉後文件上傳
我的代碼
protected void drpuser_SelectedIndexChanged(object sender, EventArgs e)
{
Guid SelectedUserId =Guid.Parse(drpuser.SelectedValue);
FileUploader();
}
FileUploader
public void FileUploader()
{
// var user = Membership.GetUser();
if (Roles.IsUserInRole("Administrator"))
{
Guid SelectedUserId = Guid.Parse(drpuser.SelectedValue); //<-- value correct
foreach (string s in Request.Files)
{
HttpPostedFile file = Request.Files[s];
int fileSizeInBytes = file.ContentLength;
string fileName = file.FileName;
string fileExtension = "";
if (!string.IsNullOrEmpty(fileName))
fileExtension = Path.GetExtension(fileName);
Guid UserGUID = (Guid)Membership.GetUser().ProviderUserKey;
string UserFolderPath = "~/UploadedFiles/" + UserGUID;
System.IO.Directory.CreateDirectory(Server.MapPath(UserFolderPath));
string savedFileName = Path.Combine(Server.MapPath(UserFolderPath), fileName);
string FullPath = UserFolderPath + "/" + fileName;
file.SaveAs(savedFileName);
DataAccess da = new DataAccess();
da.AddAdminFiles(UserGUID, FullPath, DateTime.Now, true, SelectedUserId);
}
}
else
{
}
如果您選擇文件,那麼* OnSelectedIndexChanged *事件,Request.Files.Count應該是1.您可以在foreach之前檢查此屬性。 –