我正在使用 Ajax Async File Uploader 抓取图像文件并将其保存到文件夹中:
if( file != null && file.hasFile)
{
if( file is the appropriate file type)
{
if( the filename is already taken)
{
add random numbers on the file name until it's unique
}
file.SaveAs( filepath + filename );
}
}
这在我的本地计算机上效果很好。但是,当我将程序发布到服务器时,文件将以正确的名称保存在正确的位置,但文件大小为 0。我无法打开文件。
我究竟做错了什么?
这是我的真实代码:
// If cert has a file
if(cert != null && cert.HasFile)
{
// If cert is the appropriate file type
if((cert.FileName.IndexOf(".jpg", StringComparison.OrdinalIgnoreCase) < 0) &&
(cert.FileName.IndexOf(".jpeg", StringComparison.OrdinalIgnoreCase) < 0) &&
(cert.FileName.IndexOf(".tiff", StringComparison.OrdinalIgnoreCase) < 0) &&
(cert.FileName.IndexOf(".png", StringComparison.OrdinalIgnoreCase) < 0) &&
(cert.FileName.IndexOf(".gif", StringComparison.OrdinalIgnoreCase) < 0) &&
(cert.FileName.IndexOf(".pdf", StringComparison.OrdinalIgnoreCase) < 0))
{
// Popup warning
Session["imagePopup"] = "true";
}
else
{
string filename = cert.FileName;
// If image already exists randomly add numbers until a unique filename is found
while( File.Exists(System.Configuration.ConfigurationManager.AppSettings["CertificateSavePath"] + filename))
{
Random r = new Random();
filename = filename.Insert(0, r.Next(99).ToString());
}
// Save the new file
try
{
// save the file
cert.SaveAs(System.Configuration.ConfigurationManager.AppSettings["CertificateSavePath"] + filename);
}
catch(Exception ex)
{
error.Text += ex.Message + "<br />";
}