【发布时间】:2014-05-30 12:13:29
【问题描述】:
我想用 FileUpload 上传一张图片并将其存储在数据库中,它的代码是这样的:
string filePath = FileUpload1.PostedFile.FileName;
string filename = Path.GetFileName(filePath);
string ext = Path.GetExtension(filename);
string contenttype = String.Empty;
//Set the contenttype based on File Extension
if (contenttype != String.Empty)
{
Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
//insert the file into database
f.photo = Convert.ToString(FileUpload1.FileBytes);
Label1.ForeColor = System.Drawing.Color.Green;
Label1.Text = "File Uploaded Successfully";
}
但我收到此错误:
error :Object reference not set to an instance of an object.`
in
string filename = Path.GetFileName(filePath);
这是什么原因造成的?
谢谢
【问题讨论】:
标签: asp.net file-upload webforms