클래스 수준에서 문자열 변수를 정의하고 성공적으로 파일을 업로드 한 후이 변수를 protected void UploadButton_Click(object sender, EventArgs e)
으로 설정했습니다.함수에서 클래스 수준 변수로의 값 전달 항상 null
나는이 함수에서 다른 값 fileName
을이 함수로 전달할 수 있도록 protected void btnSave_Click(object sender, EventArgs e) {}
을 데이터베이스에 저장합니다. 값은 항상 null입니다. 내가 뭔가 잘못하고 또는 기능 Protected
유형으로 정의 된대로 null이 남아 있습니까
public partial class News: System.Web.UI.Page
{
string _fileName = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// some code here......
}
}
protected void UploadButton_Click(object sender, EventArgs e)
{
if (FileUploadControl.HasFile)
{
try
{
System.IO.FileInfo f = new System.IO.FileInfo(FileUploadControl.PostedFile.FileName);
if (f.Extension.ToLower() == ".pdf" || f.Extension.ToLower() == ".doc" || f.Extension.ToLower() == ".docx")
{
//3MB file size
if (FileUploadControl.PostedFile.ContentLength < 307200)
{
string filename = Path.GetFileName(FileUploadControl.FileName);
if (!System.IO.File.Exists("../pdf/news/" + FileUploadControl.FileName))
{
FileUploadControl.SaveAs(Server.MapPath("../pdf/research/") + filename);
StatusLabel.Text = "Upload status: File uploaded!";
_fileName = FileUploadControl.FileName;
}
else
{
_fileName = null;
StatusLabel.Text = "File with this name already exsists, Please rename file and Upload gain";
}
}
else
{
_fileName = null;
StatusLabel.Text = "Upload status: The file has to be less than 3MB!";
}
}
else
{
_fileName = null;
StatusLabel.Text = "Upload status: Only PDF or Word files are accepted!";
}
}
catch (Exception ex)
{
_fileName = null;
StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
}
}
}
값 파일 이름은 동일한 클래스 내에서 액세스 할 수 있어야합니다. 코드를 디버그 했습니까? – mrsrizan
@mrsrizan, 그렇습니다. 제대로 업로드 기능에서'fileName' 변수에 값을 할당하고 디버깅을했는데 저장 버튼을 클릭하면 항상 null입니다. – Learning
정적으로 만들어야합니다. 정적 문자열 _fileName = null; – mrsrizan