struts1 上传文件 及 获取表单中的值

struts1 上传文件 及 获取表单中的值,第1张

在配置文件里配置表单,页面标签的Name要跟表单里对应起来,,然后再Action里给Form赋值,就得到了..不会写就直接写request.getParameter("userName") 这样就可以了

protected void UploadButton_Click(object sender, EventArgs e)

{

// Before attempting to save the file, verify

// that the FileUpload control contains a file.

if (FileUpload1.HasFile)

// Call a helper method routine to save the file.

SaveFile(FileUpload1.PostedFile)

else

// Notify the user that a file was not uploaded.

UploadStatusLabel.Text = "You did not specify a file to upload."

}

void SaveFile(HttpPostedFile file)

{

// Specify the path to save the uploaded file to.

string savePath = "c:\\temp\\uploads\\"

// Get the name of the file to upload.

string fileName = FileUpload1.FileName

// Create the path and file name to check for duplicates.

string pathToCheck = savePath + fileName

// Create a temporary file name to use for checking duplicates.

string tempfileName = ""

// Check to see if a file already exists with the

// same name as the file to upload.

if (System.IO.File.Exists(pathToCheck))

{

int counter = 2

while (System.IO.File.Exists(pathToCheck))

{

// if a file with this name already exists,

// prefix the filename with a number.

tempfileName = counter.ToString() + fileName

pathToCheck = savePath + tempfileName

counter ++

}

fileName = tempfileName

// Notify the user that the file name was changed.

UploadStatusLabel.Text = "A file with the same name already exists." +

"<br />Your file was saved as " + fileName

}

else

{

// Notify the user that the file was saved successfully.

UploadStatusLabel.Text = "Your file was uploaded successfully."

}

// Append the name of the file to upload to the path.

savePath += fileName

// Call the SaveAs method to save the uploaded

// file to the specified directory.

FileUpload1.SaveAs(savePath)

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12012053.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存