如何在上傳時更改文件名?更改.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它不工作......這是什麼問題? :) 謝謝
'it does not work'=?它做什麼/不做,你會得到什麼錯誤/例外? – forsvarir 2011-05-19 08:23:04
它與oFile.SaveAs(sDirectory +「/」+「test」);但不適用於oFile.SaveAs(sDirectory +「/」+ newName);所以字符串變量有什麼問題?對不起,我是新來的.net :)所以真的不知道如何捕捉異常.. – BlackJ 2011-05-19 08:34:30