如何避免避免system.io.pathtoolongexception? [英] How do ı avoid avoid system.io.pathtoolongexception?

查看:148
本文介绍了如何避免避免system.io.pathtoolongexception?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试从我的网站下载图片时出现此错误(

When I tried download image from my website ı get this error (

The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

)我需要避免

System.IO.PathTooLongException

但是无法弄明白



我的上传部分就像是



but cannot figure it out

my upload part is like

<pre>if (FileUpload1.HasFile)
        {
            string FileExtension = System.IO.Path.GetExtension(FileUpload1.FileName);
            if (FileExtension.ToLower() == ".png")
            {
                if (FileUpload1.FileBytes.Length < 10000000)
                {
                    System.IO.Stream fs = FileUpload1.PostedFile.InputStream;
                    System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
                    Byte[] bytes = br.ReadBytes((Int32)fs.Length);
                    string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
                    Image1.ImageUrl = "data:image/png;base64," + base64String;
                    Image1.Visible = true;
                    Bitmap bmp = new Bitmap(fs);
                    stegoImg = new StegoImage(bmp);
                }







我的下载部分是这样的






And My download part is like this

string url = Image1.ImageUrl;
        string FileExtension = System.IO.Path.GetExtension(FileUpload1.FileName);
        string FileName = FileUpload1.FileName;

        WebClient client = new WebClient();
        client.DownloadFile(url, Server.MapPath((FileUpload1.FileName)+FileName));





我尝试了什么:



我试图使用Delimon库,但ı无法处理



What I have tried:

I tried to use Delimon library but ı cannot handle with that

推荐答案

嗯。

查看你的代码:

Um.
Look at your code:
        string FileName = FileUpload1.FileName;
...
        client.DownloadFile(url, Server.MapPath((FileUpload1.FileName)+FileName));

所以你的文件名在那里两次为初学者;删除其中一个可能会大大缩短文件名。



除此之外,我们无法提供任何帮助,除非您移动文件夹。当您使用没有任何文件夹引用的MapPath时,您将获得一个相对于当前网页的路径,我们无法控制它,因为它将相对于您网站的根目录以及您为您选择的文件夹结构网页。通过将上传内容移动到站点根目录中的特定文件夹,您可以充分缩短名称:

So your filename is in there twice for starters; removing one of them may shorten the file name considerably.

Other than that, we can't do anything to help, unless you move your folder. When you use MapPath without any folder references, you get a path with is relative to the current webpage, and we have no control over that as it will be relative to the root directory of you website combined with the folder structure you have picked for your webpage. By moving the uploads to a specific folder in the root of your site, you may be able to shorten the name sufficiently:

client.DownloadFile(url, Server.MapPath("~/Uploads/" + FileName));



但是...我不会存储这样的文件。

这是一个网站,它本质上是多用户。如果您使用原始文件名存储文件,则会发生冲突,这意味着UserB的版本将被UserB的版本覆盖。更好的解决方案是将实际文件存储在临时名称下(GUID对此有用)并通过数据库交叉引用原始文件名和/或用户。


But ... I wouldn't store files like that.
This is a website, which by its very nature is multiuser. If you store files with the original filenames, thera are going to clash, and that means UserA's version will be overwritten by UserB's version. A better solution is to store the actual file under a temporary name (GUIDs are good for this) and cross reference the original filename and / or user via a database.


这篇关于如何避免避免system.io.pathtoolongexception?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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