File Upload and CSS

T

Todd Acheson

I'm having a small problem with uploading files in ASP.NET.

My html page for uploading has something similar to:

<form id="Form1" method="post" enctype="multipart/form-data" runat="server">
<input id="MyFile" type="file" runat="server" size="50">
<asp:Button Runat="server" ID="btnUpload" Text="UPLOAD" />
</form>

.. . .and the corresponding vb.net code:

Dim strFullPath As String
strFullPath = MyFile.PostedFile.FileName
Dim strFileName As String
strFileName = Path.GetFileName(strFullPath)
'ViewState("strDirectory") might by a folder like "db/" or "pdf/"
MyFile.PostedFile.SaveAs(ViewState("strDirectory") & strFileName)

All very straighforward stuff that we've all used before.

A file will upload just fine with the above code. The problem is, that when
the page comes back to the browser, all the CSS is lost. This is true in
both IE6 and FireFox 1.0. The design of the page(s) is a TableLess design
(no html tables). I have other projects where this worked fine, and the
CSS wasn't lost, but the upload was in an html table. Since it happens in
both of these browsers, I'm thinking it isn't browser dependent??

My question is: Is there something wrong with the above code that is triggering
the CSS to be lost? Is it a bug in ASP.NET?

As a solution, I could probably redirect the user to a different page indicating
the results of their attempted file upload.
Maybe I could try setting using:
Response.Cache.SetCacheability(HttpCacheability.NoCache)
at the top of the page?

I appreciate any advice y'all can give.

Thanks,

Todd Acheson
 
P

Patrice

The "CSS is lost" ? Do you mean that tht resulting HTML code doesn't contain
any more a reference to your external sttylesheet ?

How is this stylesheet referenced from your APSX Page ?

Patrice
 
T

Todd A

Oh, and another problem:

After the file is uploaded, if I have a link to this newly uploaded file,
and the user clicks on that link, they are prompted for WIndows Security
information (username, domain, and password).
But, if I publish that same file with FrontPage to the same directory that
it was uploaded, the user is longer prompted for security, and the link
works fine.

Any advice?
 
T

Todd Acheson

Hello Patrice,

What I mean by "CSS is lost" is that the page has all the content for the
user, but the formatting supplied by the CSS is not being applied.

I reference the CSS by:

<link href="styles.css" type="text/css" rel="stylesheet">

which is in the head of the html document.

-Todd
 
P

Patrice

When it occurs have a look at the "show source" option in the browser to see
if the html code you send is correct (in particular do you have all always
the link tag ?). Note also that it looks like you are searching the css file
in the same location than your aspx page (is this intended ? What if you
replace the name of the page in the address bar with styles.css ? Can you
get the file ?)

Patrice

--
 
T

Todd Acheson

Patrice,

I can still get the CSS file by typing it in the address bar.

When I look at the source of the page (View Source) in IE, the html for the
CSS link is missing!

Well now I know why the CSS is not being applied. The question becomes,
why?


-Todd
 
T

Todd Acheson

I fixed the problem of losing my CSS reference. The tag for the link to the
stylesheet is in a user control. The code for making the tag was within a
If Not Page.IsPostBack block of code. Removing this If statement fixed the
problem.

I'm still wondering why the users are prompted for Windows security when
clicking on a link to user uploaded files? Files published with FrontPage
don't have this problem.

My web.config has the authentication set to "none".

Todd Acheson
 
G

Guest

Check the permissions on the uploaded files and make sure read access is
allowed. I think the account that needs access is "IUSR_<machinename>" but I
might be wrong about that.
 
B

BW

Todd,

When they click on the link, they're basicly downloading the file right? I
think what you have to do is get the browser to display the download box
instead of trying to open the file. I'm not sure of the correct term for it
but you basicly stream the attachment. Something like this:

the file link calls the FileDownload.aspx page like...: <a
href=FileDownload.aspx?file=yourFileWithFullPathAndExtension>File Name</a>

<%@ Import Namespace="System.IO"%>
<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)

Dim filepath As String = Request.Params("file")
If Not filepath Is Nothing Then
If File.Exists(filepath) Then
Dim filename As String = Path.GetFileName(filepath)
Response.Clear()
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", _
"attachment; filename=""" & filename & """")
Response.Flush()
Response.WriteFile(filepath)
End If
End If

End Sub
</script>

This works fine for me. I hope this helps. re:
http://www.ondotnet.com/lpt/a/1354

Bernard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top