?
Solved

saving file

Posted on 2006-05-12
6
Medium Priority
?
291 Views
Last Modified: 2013-12-25
this is in refernce to
http://www.experts-exchange.com/Web/Web_Languages/CGI/Q_21847334.html

thsi is used to upload file on server.........but how to save the file on client's machine......
0
Comment
Question by:perlperl
  • 5
6 Comments
 

Author Comment

by:perlperl
ID: 16667572
index.cgi

#!c:/perl/bin/Perl.exe
use CGI;

$query = new CGI;
print $query->header(); # it is easier to creat HTML headers with these CGI methods
print $query->start_html();

print $query->start_multipart_form(-method=>"POST", -action=>"test2.cgi"),
      "File to upload:",
      $query->filefield(-name=>'upload_file',  -size=>40),  # let user pick a lcoal file
      p,
      $query->submit(-name=>'submit',  -value=>'Upload File'),
      $query->endform,
      $query->end_html;





test2.cgi
#!c:/perl/bin/Perl.exe
use CGI;

print "Content-type: text/html; charset=iso-8859-1\n\n";
$|=1;
$query = new CGI;
$filename = $query->param("upload_file");
print "$filename  : <br>";
$filename =~ s/.*[\/\\](.*)/$1/;
$upload_filehandle = $query->upload("upload_file");
$upload_dir = "C:\\file.doc";

open UPLOADFILE, ">$upload_dir";
binmode UPLOADFILE;
 while ( <$upload_filehandle> )
 {
   print UPLOADFILE;
 }
 close UPLOADFILE;

#### i will do some processing here

### how do i save the file back to the client's machine, the file is in my cgi-bin directory

0
 

Author Comment

by:perlperl
ID: 16667659
the above code works fine in uploading the file from clients machin to server.....i save the file as file.doc on server...then i modify the file file.doc on server...but in the end i want to save the file file.doc back to client's machine
0
 

Author Comment

by:perlperl
ID: 16667746
i tried adding this in the end

$ID = "C:\\file.doc";
open(DLFILE, "<$ID") || Error('open', 'file');
binmode DLFILE;
@fileholder = <DLFILE>;  
close (DLFILE) || Error ('close', 'file');  

print "Content-Type:application/x-download\n";  
print "Content-Disposition:attachment;filename=$ID\n\n";
print @fileholder ;

it is simply displaying junk characters of wordfile on the browser instead of downloading the attachment on client's machine
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:perlperl
ID: 16667885
i also tried this

but this also display the file content on browser instead of saving the file


#!c:/perl/bin/Perl.exe
use CGI;

print "Content-type: text/html; charset=iso-8859-1\n\n";
$|=1;

#print "Upload File.........\n";
$query = new CGI;
$filename = $query->param("upload_file");
#print "$filename  : <br>";
$filename =~ s/.*[\/\\](.*)/$1/;
$upload_filehandle = $query->upload("upload_file");
$upload_dir = "C:\\file.doc";

open UPLOADFILE, ">$upload_dir";
binmode UPLOADFILE;
 while ( <$upload_filehandle> )
 {
   print UPLOADFILE;
 }
 close UPLOADFILE;


my $q = new CGI;
my $path = 'C:';
my $fname = "file.doc";
my $filepath = "$path\\$fname";
my $size= -s $filepath;
my $buff;

print "Content-type: application/octet-stream\r\n";
print "Content-Length: $size\r\n";
print "Content-disposition: attachment;filename=$fname\r\n\r\n";
open(FILE, "<$filepath") || die;
binmode FILE;
binmode STDOUT;
while (read FILE, $buff, 1024) {
  print $buff;
}
close(FILE);
0
 

Author Comment

by:perlperl
ID: 16667958
the last option i tried is

$f = "http://servername:8080/cgi-bin/file.doc";
print qq(
<script language="javascript">
window.location = "$f"
</script>
);

this worked fine, it opens the file in word directly..which i can save

bust is there a way, i can get the normal promt, "would you like to open or save" dialog box, then user clicks save to save file
the above method is just quick and dirty
0
 
LVL 19

Accepted Solution

by:
Kim Ryan earned 1500 total points
ID: 16673130
The filefield is only for letting a user borwse their local (client) file system and upload to the server. For downloads you need a different approach, not form based.

You are on the right track. After the file has been modified, you need to generate a html page that contains a href to the new file. It may also help to add some text such as click this link to download modified doc. The href is just

<a href="http://servername:8080/cgi-bin/file.doc">Click to download new file.doc</a>

The browser should give the user option to either save this file to disk or open it with word. Depending on how it is set up it may just try to start word if it is installed on client PC. Thereis always the option to tell the user to right-click on link and select the 'Save Targe As' option.

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question