开发者

Change filename on file upload in .net (asp)

How to change file name on upload ?

I have such code :

<%@ 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(sDirect开发者_如何学编程ory)) Directory.CreateDirectory(sDirectory);
     context.Response.Write("1");
   }
   public bool IsReusable {
      get { return false; }
   }
}

And if i change oFile.Filename to newName it does not work ... what is the problem ? :) Thank you


You can pass your Custom File Name along with Directory to SaveAs Method

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


try:

 // 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 );


I haven't tried this code but I do want to point out two things of from the original code:

The first is this order of operations:

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

I believe it should be this instead. In the above sequence, there is a potential edge case of saving into a non-existing folder. This ensures that the folder is created:

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

The other thing is that you might be running into issues with / as the path separator. I think it should be much safer to do something like:

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

I hope this helps!


Here is an example i used when saving an image look at the save as section

////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;


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.";
        }


private string UpdateFilename(string filename)
    {
        try
        {
            filename = Server.HtmlEncode(FUJD.FileName);
            string extension = System.IO.Path.GetExtension(filename);
            filename = filename.Replace(extension, "");
            return filename + '-' + DateTime.Now.ToString("yyyyMMddHHmmss") + extension;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜