开发者

save multiple image to local from folder in asp.net

开发者 https://www.devze.com 2023-03-28 04:56 出处:网络
ive got a file download issue can you help me for that... here is the code: DirectoryInfo directoryInfo = new DirectoryInfo(Server.MapPath(@\"/Bailiffs/BailiffFiles/\"));

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


                }              开发者_如何学Go      
            }
            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...


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();
0

精彩评论

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

关注公众号