Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing & Reading Files on the Web Server 3

Status
Not open for further replies.

mikie15

MIS
May 15, 2004
6
US
I have an assignment and I'm stuck.
I have to upload an image to a text file. No problem.
But then I have to read that file and be able to click on the image name and view that image. That's where I'm stuck.
Here is the code to reading the text file, but how do I code it to made the file names either hyperlinks or if when the file opens the file names are viewed as the images. Any help would be greatly appreciated.

<%@ Page Language="VB" %>
<%@ import Namespace="System.IO" %>
<% @ import Namespace="System.Web.UI.Webcontrols" %>
<script runat="server">

Sub Page_Load(Source as Object, e as EventArgs)
dim fs as new FileStream(Server.MapPath("files.txt"), FileMode.Open, FileAccess.Read)
dim objReader as new StreamReader(fs)


While objReader.Peek() > -1 'Test to for end of file
Response.Write(objReader.ReadLine & "<br>")
End While

objReader.Close
fs.close
End Sub
 
mikie - this seems like an interesting problem; have some time today to take a look at it. Not exactly sure what you mean when you say "upload an image to a file".

Do you upload the path? Just the name? Clarify exactly how you 'upload the images to a text file' and I'll take a look and see what I can come up with.
 
Here is the code for uploading the file. Basically theres a text box and a browse button to go and choose and file and then once you hit upload it saves that file to a new location.

<%@ Page Language="VB" %>
<%@ import namespace="System" %>
<%@ import namespace="System.Web" %>
<%@ import namespace="System.IO" %>

<script language="VB" runat="server">

Sub Upload_File(Source as Object, e as EventArgs)
Dim strFullPath as string
Dim strFileName as string
Dim intPosition as integer

'Full path with file name
strFullPath=file1.postedfile.filename

'Find the position of the last slash - filename is after it
intPosition=strFullPath.lastindexof("\") + 1


'Get the filename
strFileName=StrFullPath.substring(intPosition)

If file1.postedfile.contentlength > 50000 then
lblMessage.Text = "Your file is larger than 50k. Please reduce the size and try again."
Else
'Server location for file (if you want to use a different folder insert the path in
'between the quotes of Server.MapPath("your\path\")
file1.postedfile.saveas(Server.MapPath("") & "\" & strFileName)

lblMessage.Text = "Your file has been uploaded as " & Server.MapPath("") & "\" & strFileName
lblFullPath.Text = "Full Path=" & file1.postedfile.filename
lblFileSize.text = "File Size=" & file1.postedfile.contentlength
lblFiletype.text = "File Type=" & file1.postedfile.contenttype
end if

dim objWriter as new StreamWriter(Server.MapPath("Files.txt"), true)
objWriter.WriteLine(strFileName)
objWriter.Close
End sub
</script>

<html>
<body>
<form runat="server" enctype="multipart/form-data">
<h3>Upload Images to the Server</h3>
<table>
<tr>
<td>Select File</td>
<td><input type="file" id="file1" runat="server" size="80" /> </td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<asp:Button id="btn1" runat="server" text="Upload" OnClick="Upload_File" /></td>
</tr>
</table>
<p>
<asp:label id="lblMessage" runat="server" /><br>
<asp:label id="lblFullPath" runat="server" /><br>
<asp:label id="lblFileSize" runat="server" /><br>
<asp:label id="lblFileType" runat="server" /><br>
</form>


<p><a href="readfile.aspx">readfile.aspx</a></p>


</body>
</html>
 
mikie: What I meant (should have been a bit clearer on this) is what exactly is in the text file that is uploaded?

e.g., say you upload:

C:/Files/Myfile.text

containing, e.g.,

C:\Photos\Photo1.jpg
C:\Photos\Photo2.jpg
C:\Photos\Photo3.jpg
...

i.e., if you are uploading text file, what is in this text file?
 
just the image file name is in the text file.

for example:

j0275738.wmf
j0275772.wmf

These image files are located on the server in the same folder as the read01.aspx file.

 
mikie: give this a try...

Code:
<%@ Page Language="VB" %>
<%@ import Namespace="System.IO" %>
<% @ import Namespace="System.Web.UI.Webcontrols" %>
<script runat="server">
Sub Page_Load(Source as Object, e as EventArgs)
 dim fs as new FileStream(Server.MapPath("Photos/files.txt"), FileMode.Open, FileAccess.Read)
 dim objReader as new StreamReader(fs)
 dim strFile As String
 While objReader.Peek() > -1 'Test to for end of file                
  strFile = objReader.Readline
  Response.Write(strFile & " <a href=" & strFile & _
  ">" & "Click here to open pic" & "</a>" & "<br>")
 End While
 objReader.Close
 fs.close
End Sub
</script>
<HTML>
<HEAD>
<TITLE>Test Page</TITLE>
</HEAD>
<body>
</BODY>
</HTML>
 
That will work. Thank you sooooooo much. You are awsome.
 
Another approach that might expose you to more of the powers of .NET controls (though it would take more time to learn about) would be to do this:

1. Create code to make an array or XmlDocument based on the names in your file.

2. On your page, place an Image control, and a DataList control.

3. Right-click the DataList, and chose the option to edit the "item template". Put a Hyperlink in said template.

4. In the template markup, have the NavigateURL property of the hyperlink be your page name plus a QueryString value bound to the values in your array or XmlDocument. Bind at the appropriate time.

5. Bind the ImageURL property of the Image control to the QueryString value (or set it in the Page.Load event).

Voila! ...

Actually after writing all of that, I realized a lot of that may be a little overwhelming depending on what you have learned so far.

Anyhoo... Isadore, I think he meant to give you a star, so you'll get one from me instead (if I can get it to work... it seems to be down or something).



 
Boulder --

Hey thanks. I'm going to take a look at your analysis. I'm glad people like yourself, Paul, Crazyboy, dragon and others are around. I've never had a course or any training in dot NET (or any other programing language - one Fortran class 20 years ago) and so I am learning much from you guys. Textbooks help a lot, but of course time is restrictive with other things to do. People like myself and Mikie gain valuable insight into your suggestions, and we really appreciate that.

Thanks for your time Boulder; believe me, sharing your knowledge in this area helps many. Keep up the good work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top