在.net(asp)中更改文件上传文件名

时间:2011-05-19 08:20:11

标签: c# asp.net

如何在上传时更改文件名?

我有这样的代码:

<%@ 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 != "<!--@Ecom: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则不起作用......问题是什么? :) 谢谢

5 个答案:

答案 0 :(得分:0)

您可以将自定义文件名和目录一起传递给SaveAs方法

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

答案 1 :(得分: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 != "<!--@Ecom:productCombinationString-->") {
           newName = newFileName1 + newFileName2 + extension ;
       } else {
           newName = newFileName1 + extension ; 
       }

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

答案 2 :(得分: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;

答案 3 :(得分: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);

我希望这有帮助!

答案 4 :(得分: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.";
        }