2011-05-19 75 views
-1

如何在上傳時更改文件名?更改.net文件上傳文件的文件名(asp)

我有這樣的代碼:

<%@ WebHandler Language="C#" Class="Upload" %> 

using System; 
using System.Web; 
using System.IO; 

public class Upload : IHttpHandler { 
    public void ProcessRequest(HttpContext context) { 
     HttpPostedFile oFile = context.Request.Files["Filedata"]; 
    string newFileName1 = HttpContext.Current.Server.MapPath(@context.Request["orderID"]); 
    string newFileName2 = HttpContext.Current.Server.MapPath(@context.Request["productCombinationString"]); 
    string newName; 
     if(newFileName2 != "" && newFileName2 != null && newFileName2 != "<[email protected]:productCombinationString-->") { 
      newName = newFileName1 + newFileName2 + oFile.ContentType; 
     } else { 
      newName = newFileName1 + oFile.ContentType; 
     } 

    string sDirectory = HttpContext.Current.Server.MapPath(@context.Request["folder"]); 
    oFile.SaveAs(sDirectory + "/" + oFile.FileName); 
    if (!Directory.Exists(sDirectory)) Directory.CreateDirectory(sDirectory); 
    context.Response.Write("1"); 
    } 
    public bool IsReusable { 
     get { return false; } 
    } 
} 

如果我改變oFile.Filename爲NewName它不工作......這是什麼問題? :) 謝謝

+2

'it does not work'=?它做什麼/不做,你會得到什麼錯誤/例外? – forsvarir 2011-05-19 08:23:04

+0

它與oFile.SaveAs(sDirectory +「/」+「test」);但不適用於oFile.SaveAs(sDirectory +「/」+ newName);所以字符串變量有什麼問題?對不起,我是新來的.net :)所以真的不知道如何捕捉異常.. – BlackJ 2011-05-19 08:34:30

回答

0

您可以使用目錄轉達您的自定義文件名SaveAs方法

oFile.SaveAs(sDirectory + "/" + "abc"); 
+0

我如何說:如果我改變oFile.SaveAs(sDirectory +「/」+ oFile.FileName); to oFile.SaveAs(sDirectory +「/」+ newName);它不工作... – BlackJ 2011-05-19 08:28:13

+0

實際上這工作...所以最新的字符串變量錯了嗎?因爲oFile.SaveAs(sDirectory +「/」+ newName); dows不工作 – BlackJ 2011-05-19 08:32:07

+0

它也應該工作字符串變量。調試並檢查文件和目錄變量的值。 – 2011-05-19 08:34:19

0

嘗試:

// Get the extension of the uploaded file. 
string fileName = Server.HtmlEncode(FileUpload1.FileName); 
    string extension = System.IO.Path.GetExtension(fileName); 
string newName; 
     if(newFileName2 != "" && newFileName2 != null && newFileName2 != "<[email protected]:productCombinationString-->") { 
      newName = newFileName1 + newFileName2 + extension ; 
     } else { 
      newName = newFileName1 + extension ; 
     } 

    oFile.SaveAs(sDirectory + "/" + newName); 
+0

不起作用,只有當我將newName更改爲ex的某個字符串時它才起作用。 oFile.SaveAs(sDirectory +「/」+「test」);這工作,所以什麼是錯誤的字符串變量? – BlackJ 2011-05-19 08:33:19

+0

你能檢查你在「newName」中得到什麼嗎? oFile.ContentType不會給你文件的擴展名。您必須生成具有擴展名的正確文件名(即.xls,.pdf,.zip) – 2011-05-19 09:30:02

0

這裏是保存圖像看看當我用一個例子保存爲部分

////saving file in the physical folder; 

FileUpload FileUpload1 = file_Image; 

string virtualFolder = "~/Resourceimages/"; 

string physicalFolder = HostingEnvironment.MapPath(virtualFolder); 

string PhotoName = ((string)Session["UserName"] + (string)Session["UserSurname"]); 

FileUpload1.SaveAs(physicalFolder + PhotoName + FileUpload1.FileName); 

string location = virtualFolder + PhotoName + FileUpload1.FileName; 

webservice.UpdateResourceImage((int)Session["UserID"], location); 

lbl_Result.Text = "Your file " + FileUpload1.FileName + " has been uploaded."; 

Image1.Visible = true; 
Image1.ImageUrl = location; 
0

我還沒有試過這段代碼但我想從原碼點出了兩件事情:

首先是這一操作順序:

oFile.SaveAs(sDirectory + "/" + oFile.FileName); 
if (!Directory.Exists(sDirectory)) Directory.CreateDirectory(sDirectory); 

我認爲應該是這樣吧。在上述順序中,存在潛在的邊緣情況,即將其保存到不存在的文件夾中。這確保了文件夾中創建:

if (!Directory.Exists(sDirectory)) 
{ 
     Directory.CreateDirectory(sDirectory); 
} 
oFile.SaveAs(sDirectory + "/" + oFile.FileName); 

的另一件事是,你可能會運行到問題與/作爲路徑分隔符。我認爲這樣做應該是更安全的做這樣的事情:

var saveLocation = Path.Combine(sDirectory, oFile.FileName); 
oFile.SaveAs(saveLocation); 

我希望這有助於!

0
string uploadFolder = Request.PhysicalApplicationPath + "UploadFile\\"; 
     if (FileUpload1.HasFile) 
     { 
      string extension = Path.GetExtension(FileUpload1.PostedFile.FileName); 
      FileUpload1.SaveAs(uploadFolder + "Test"+ extension); 
      Label1.Text = "File uploaded successfully as: " + "Test"+ extension; 
     } 
     else 
     { 
      Label1.Text = "First select a file."; 
     }