2016-12-15 59 views
0

我無法上傳圖像到指定的文件夾。這是代碼。將圖像上傳到桌面的文件夾

protected void btnSubmit_Click(object sender, EventArgs e) 
{ 
    if (FileUploader.HasFile) 
    { 
     try 
     { 
      string filename = Path.GetFileName(FileUploader.FileName); 
      FileUploader.SaveAs(@"D:\Users\SGG90745\Desktop\PICTURES" + filename); 
      Label1.Text = "Uploaded Successfully!!"; 
     } 
     catch (Exception ex) 
     { 
      Label1.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message; 
     } 
    } 
} 

當我點擊上傳,標籤不說Uploaded Successfully!!寫在代碼,但該圖片不是在代碼中的指定文件夾。請幫助謝謝你!

+0

你檢查文件名的價值?我假設它不是以\開頭 - 那麼FileUploader.SaveAs中提供的文件名不會將數據存儲在PICTURES文件夾內,而是存儲在Desktop文件夾中,文件名以PICTURES開頭。 –

+1

是的,看到你的桌面文件名以PICTURES_開頭。上傳的文件在桌面上? – Saadi

回答

1

放一個\照片後:

protected void btnSubmit_Click(object sender, EventArgs e) 
{ 
    if (FileUploader.HasFile) 
    { 
     try 
     { 
      string filename = Path.GetFileName(FileUploader.FileName); 
      FileUploader.SaveAs(@"D:\Users\SGG90745\Desktop\PICTURES\" + filename); 
      Label1.Text = "Uploaded Successfully!!"; 
     } 
     catch (Exception ex) 
     { 
      Label1.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message; 
     } 
    } 
} 
1

嘗試更改您的代碼

protected void btnSubmit_Click(object sender, EventArgs e) 
{ 
    if (FileUploader.HasFile) 
    { 
     try 
     { 
      string filename = Path.GetFileName(FileUploader.FileName); 
      FileUploader.SaveAs(@"D:\Users\SGG90745\Desktop\PICTURES\" + filename); 
      Label1.Text = "Uploaded Successfully!!"; 
     } 
     catch (Exception ex) 
     { 
      Label1.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message; 
     } 
    } 
} 

我只是爲了建立正確的文件名添加一個反斜槓照片後。

1

問題是這一行:

FileUploader.SaveAs(@"D:\Users\SGG90745\Desktop\PICTURES" + filename); 

添加斜線會修:

FileUploader.SaveAs(@"D:\Users\SGG90745\Desktop\PICTURES\" + filename); 

更平臺無關的方式修復它將是:

const string folder = @"D:\Users\SGG90745\Desktop\PICTURES" 
... 
var path = folder + Path.DirectorySeparatorChar + filename; 

而最好的方式做到這一點是:

const string folder = @"D:\Users\SGG90745\Desktop\PICTURES" 
... 
var path = Path.Combine(folder, filename); 
相關問題