如何从物理位置检索保存的文件名(没有guid) [英] How to retrieve back the saved file name(without guid) from physical location

查看:84
本文介绍了如何从物理位置检索保存的文件名(没有guid)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅下面的代码段。我上传了文件&保存在物理文件夹中,guid附加到文件名。请看下面。我的目的是在没有guid的情况下取回文件名,如下所示。

Please see below the code snippet. I had uploaded files & saved in physical folder with guid appended to the file name. Please see below. My purpose is to get back the file names without guid as shown below.

HttpPostedFile PostedFile = Request.Files[i];
if (PostedFile.ContentLength > 0)
{
    Guid guid = Guid.NewGuid();
    string fileNameOrg = Path.GetFileName(PostedFile.FileName);
    string extention = Path.GetExtension(fileNameOrg);
    string fileNameNew = fileNameOrg.Replace(extention, "") + guid + extention;
    PostedFile.SaveAs(path + @"\" + fileNameNew);
}



已保存的文件名:text1eb4617fd-4876-4c52-aa5b-21f8be8fb0f8.txt

已保存的文件名:text296a712ff-efd9 -4ab9-9f38-801e0fd23272.txt



现在我的目的是只提取没有GUID的文件名

那是



输出文件名:text1.txt

输出文件名:text2.txt



在此先感谢。


SAVED FILE NAME : text1eb4617fd-4876-4c52-aa5b-21f8be8fb0f8.txt
SAVED FILE NAME : text296a712ff-efd9-4ab9-9f38-801e0fd23272.txt

Now My purpose is to extract only the file name without GUID
That is

OUTPUT FILE NAME : text1.txt
OUTPUT FILE NAME : text2.txt

Thanks in advance.

推荐答案

简单:

Easy:
string inp = "text1eb4617fd-4876-4c52-aa5b-21f8be8fb0f8.txt";
const int guidLen = 36;
int extLen = inp.Length - inp.LastIndexOf('.');
string filename = inp.Substring(0, inp.Length - (extLen + guidLen)) + Path.GetExtension(inp);


为了提高效率,您可以重新考虑保存文件名,如下所示:



string fileName = Guid.NewGuid()。ToString()+ - + Path.GetFileName(PostedFile.FileName);



那样,为了稍后确定原始上传的文件名,你需要的只是从guid和dash结尾处开始的子字符串。



我之所以这么说 - 想象1,000,000名用户同时上传10个文件:-)



Rene。
To make things more efficient, you might reconsider saving the file name as follows:

string fileName = Guid.NewGuid().ToString() + "-" + Path.GetFileName(PostedFile.FileName);

That way, to determine the original uploaded file name later, all you need is the substring starting at the end of the guid and dash.

The reason I say this - imagine 1,000,000 users uploading 10 files each at the same time :-)

Rene.


这篇关于如何从物理位置检索保存的文件名(没有guid)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆