将多个图像从 asp.net 中的文件夹保存到本地

发布于 2024-11-29 15:27:32 字数 1742 浏览 0 评论 0原文

我遇到了文件下载问题,你能帮我解决这个问题吗...

这是代码:

    DirectoryInfo directoryInfo = new DirectoryInfo(Server.MapPath(@"/Bailiffs/BailiffFiles/"));
        string cukurNumber = string.Empty;
        if (txtCukurNumber.Text != string.Empty) {
            cukurNumber = txtCukurNumber.Text;
        }
        FileInfo[] fileInfoEnum = directoryInfo.GetFiles(cukurNumber + "*");
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment;filename=" + txtCukurNumber.Text + ".zip");            
        Response.ContentType = "application/zip";
        using (ZipOutputStream zipStream = new ZipOutputStream(Response.OutputStream)) {
            zipStream.SetLevel(9);
            byte[] zipBuffer = new byte[4096];

            foreach (FileInfo fileInfo in fileInfoEnum) {
                string fileFullName = fileInfo.FullName;

                ZipEntry zipEntry = new ZipEntry(Path.GetFileName(fileFullName));

                zipEntry.DateTime = DateTime.Now;

                zipStream.PutNextEntry(zipEntry);

                using (FileStream fileStream = File.OpenRead(fileFullName)) {
                    int sourceBytes = 0;
                    do {
                        sourceBytes = fileStream.Read(zipBuffer, 0, zipBuffer.Length);
                        zipStream.Write(zipBuffer, 0, sourceBytes);
                    } while (sourceBytes > 0);


                }                    
            }
            zipStream.Finish();
            zipStream.Close();
            Response.Flush();
            Response.End();
        }

此代码必须通过过滤器获取所有图像文件并将其保存到磁盘,但浏览器的保存文件对话框仅打开一次,并且一个奇怪的文件是保存...我在哪里做错了...

谢谢..

编辑:奇怪的文件问题现在已经解决,主要问题是单个文件保存而不是多个.. 再次感谢...

ive got a file download issue can you help me for that...

here is the code:

    DirectoryInfo directoryInfo = new DirectoryInfo(Server.MapPath(@"/Bailiffs/BailiffFiles/"));
        string cukurNumber = string.Empty;
        if (txtCukurNumber.Text != string.Empty) {
            cukurNumber = txtCukurNumber.Text;
        }
        FileInfo[] fileInfoEnum = directoryInfo.GetFiles(cukurNumber + "*");
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment;filename=" + txtCukurNumber.Text + ".zip");            
        Response.ContentType = "application/zip";
        using (ZipOutputStream zipStream = new ZipOutputStream(Response.OutputStream)) {
            zipStream.SetLevel(9);
            byte[] zipBuffer = new byte[4096];

            foreach (FileInfo fileInfo in fileInfoEnum) {
                string fileFullName = fileInfo.FullName;

                ZipEntry zipEntry = new ZipEntry(Path.GetFileName(fileFullName));

                zipEntry.DateTime = DateTime.Now;

                zipStream.PutNextEntry(zipEntry);

                using (FileStream fileStream = File.OpenRead(fileFullName)) {
                    int sourceBytes = 0;
                    do {
                        sourceBytes = fileStream.Read(zipBuffer, 0, zipBuffer.Length);
                        zipStream.Write(zipBuffer, 0, sourceBytes);
                    } while (sourceBytes > 0);


                }                    
            }
            zipStream.Finish();
            zipStream.Close();
            Response.Flush();
            Response.End();
        }

this code must be get all image files by filter and save to disk but save file dialog of browser is opening just one time and one bizarre file is saving... where am i doing wrong...

thanks..

Edit : Bizzarre file issue is solved now the main issue is single file saving instead of multiple..
thanks again...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疏忽 2024-12-06 15:27:33

尽管您正在循环遍历目录中的每个文件,但一旦您在循环的第一次迭代中执行了 Response.End(),对用户的响应就完成了。他们只会获取枚举器找到的第一个文件。

浏览器没有以您尝试的方式接收多个文件的概念。

您可以考虑收集各种图像文件并将它们放在一个 ZIP 文件中,然后将单个 ZIP 返回给用户。

下面是示例代码,它将构建图像的 ZIP(使用 SharZipLib)并回复名为“images.zip”的单个文件

包括 SharpZipLib 的这些 using 语句:

using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;

然后在要流回 ZIP 文件的方法中:

DirectoryInfo directoryInfo = new DirectoryInfo(Server.MapPath(@"/Bailiffs/BailiffFiles/"));
string cukurNumber = string.Empty;
if (txtCukurNumber.Text != string.Empty) {
    cukurNumber = txtCukurNumber.Text;
}

IEnumerable<FileInfo> fileInfoEnum = directoryInfo.EnumerateFiles( cukurNumber + "*" );

    Response.Clear();

Response.AddHeader( "Content-Disposition", "attachment;filename=images.zip" );
Response.ContentType = "application/zip";

using( ZipOutputStream zipstream = new ZipOutputStream( Response.OutputStream ) ) {

    zipstream.SetLevel( 9 ); // 0-9, 9 being the highest compression

    byte[] buffer = new byte[4096];

    foreach( FileInfo fileInfo in fileInfoEnum ) {

        string file = fileInfo.FullName;

        ZipEntry entry = new
        ZipEntry( Path.GetFileName( file ) );

        entry.DateTime = DateTime.Now;
        zipstream.PutNextEntry( entry );

        using( FileStream fs = File.OpenRead( file ) ) {
            int sourceBytes;
            do {
                sourceBytes = fs.Read( buffer, 0, buffer.Length );
                zipstream.Write( buffer, 0, sourceBytes );
            } while( sourceBytes > 0 );
        }
    }

    zipstream.Finish();
    zipstream.Close();
}

Response.Flush();
Response.End();

Although you are looping over each file in the directory, once you do your Response.End() on the first iteration of the loop, the response to the user is done. They would only get the first file that is found by the enumerator.

The browser doesn't have a concept of receiving multiple files in the way you are attempting.

You may consider collecting the various image files and putting them together in a ZIP file, and then returning a single ZIP back to the user.

Here is example code that will build a ZIP (using SharZipLib) of the images and reply with a single file called "images.zip"

Include these using statements for SharpZipLib:

using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;

Then in the method where you want to stream back the ZIP file:

DirectoryInfo directoryInfo = new DirectoryInfo(Server.MapPath(@"/Bailiffs/BailiffFiles/"));
string cukurNumber = string.Empty;
if (txtCukurNumber.Text != string.Empty) {
    cukurNumber = txtCukurNumber.Text;
}

IEnumerable<FileInfo> fileInfoEnum = directoryInfo.EnumerateFiles( cukurNumber + "*" );

    Response.Clear();

Response.AddHeader( "Content-Disposition", "attachment;filename=images.zip" );
Response.ContentType = "application/zip";

using( ZipOutputStream zipstream = new ZipOutputStream( Response.OutputStream ) ) {

    zipstream.SetLevel( 9 ); // 0-9, 9 being the highest compression

    byte[] buffer = new byte[4096];

    foreach( FileInfo fileInfo in fileInfoEnum ) {

        string file = fileInfo.FullName;

        ZipEntry entry = new
        ZipEntry( Path.GetFileName( file ) );

        entry.DateTime = DateTime.Now;
        zipstream.PutNextEntry( entry );

        using( FileStream fs = File.OpenRead( file ) ) {
            int sourceBytes;
            do {
                sourceBytes = fs.Read( buffer, 0, buffer.Length );
                zipstream.Write( buffer, 0, sourceBytes );
            } while( sourceBytes > 0 );
        }
    }

    zipstream.Finish();
    zipstream.Close();
}

Response.Flush();
Response.End();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文