简体   繁体   English

有没有一种方法可以重命名上载的文件而不保存它?

[英]Is there a way to rename the uploaded file without saving it?

I tried looking into other solutions , however they suggest either: 我尝试研究other solutions ,但是他们建议:

  1. to save the file with a different name with saveAs() saveAs()用不同的名称save the file
  2. to change the file name once the file is saved with Move() or Copy() 使用Move()Copy() once the file is saved文件once the file is saved更改文件名

In my case I need to rename it without saving it . 就我而言, I need to rename it without saving it I tried changing the file.FileName property, however it is ReadOnly . 我尝试更改file.FileName属性,但是它是ReadOnly

The result I'm trying to get is: 我想要得到的结果是:

public HttpPostedFileBase renameFiles(HttpPostedFileBase file)
{
    //change the name of the file
    //return same file or its copy with a different name
}

It would be good to have HttpPostedFileBase as a return type , however it can be sacrificed if needed. 这将是good to have HttpPostedFileBasereturn type ,但是它can be sacrificed如果需要的话。

Is there a way to do this through memory streams or anything else? 有没有办法通过memory streams或其他方式做到这一点? I appreciate any help, thank you for taking your time to read this. 感谢您的帮助,感谢您抽出宝贵的时间阅读本文档。 :) :)

Short Answer: NO 简短答案:

Long Answer: You can rename a file only if file exists on File System. 长答案:仅当文件系统上存在文件时,才可以重命名文件。

The files uploaded are not files at all - when you access them using Request.Files. 上载的文件根本不是文件-当您使用Request.Files访问它们时。 They are streams. 他们是溪流。 The fileName property is readonly because of the same reason. 由于相同的原因,fileName属性为只读。

There is no name associated with stream. 没有与流关联的名称。

As per documentation, FileName property 根据文档,FileName属性

Gets the fully qualified name of the file on the client. 获取客户端上文件的标准名称。

Well, I finally found a way that's really simple - I guess I was overthinking this a bit. 好吧,我终于找到了一种非常简单的方法-我想我对此有点想过。 I figured I'll just share the solution, since some of you might need it. 我想我将只分享解决方案,因为其中一些人可能需要它。 I tested it and it works for me. 我测试了它,对我有用。

You just have to create your own class HttpPostedFileBaseDerived that inherits from HttpPostedFileBase . 你只需要create your own class HttpPostedFileBaseDerived从继承HttpPostedFileBase The only difference between them is that you can make a constructor there. 它们之间的唯一区别是您可以在那里创建一个构造函数。

    public class HttpPostedFileBaseDerived : HttpPostedFileBase
    {
        public HttpPostedFileBaseDerived(int contentLength, string contentType, string fileName, Stream inputStream)
        {
            ContentLength = contentLength;
            ContentType = contentType;
            FileName = fileName;
            InputStream = inputStream;
        }
        public override int ContentLength { get; }

        public override string ContentType { get; }

        public override string FileName { get; }

        public override Stream InputStream { get; }

        public override void SaveAs(string filename) { }

    }
}

Since constructor is not affected by ReadOnly , you can easily copy in the values from your original file object to your derived class's instance , while putting your new name in as well: 由于constructor is not affected by ReadOnly ,因此您可以轻松地copy in the values from your original file对象copy in the values from your original file to derived class's instance ,同时也将新名称放入其中:

HttpPostedFileBase renameFile(HttpPostedFileBase file, string newFileName)
{
    string ext = Path.GetExtension(file.FileName); //don't forget the extension

    HttpPostedFileBaseDerived test = new HttpPostedFileBaseDerived(file.ContentLength, file.ContentType, (newFileName + ext), file.InputStream);
    return (HttpPostedFileBase)test; //cast it back to HttpPostedFileBase 
}

Once you are done you can type cast it back to HttpPostedFileBase so you wouldn't have to change any other code that you already have. 完成后,您可以type casttype cast HttpPostedFileBaseHttpPostedFileBase这样就不必更改任何其他已有的代码。

Hope this helps to anyone in the future. 希望这对将来的任何人有帮助。 Also thanks to Manoj Choudhari for his answer, thanks to I learned of where not to look for the solution. 同时还要感谢Manoj Choudhari的回答,也要感谢我了解到哪里不寻求解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM