Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

File Download Dialogue box

Posted on 2009-05-11
4
Medium Priority
?
771 Views
Last Modified: 2012-05-06
Hi!
I have a generic download page which all my download links will point to.
It will take in some paremeters to do some checking but that is not the problem.

I need to do a "File Download" dialogue box which will allow the user to either save or open.

What i am encountering is...
If i upload a 2MB file, if i chose to save or open in the file download dialogue box, the file will be corrupted.
If i choose to save the file down, it will NOT be 2MB but maybe 1.6MB.

Any advice?
SPWeb web = SPContext.Current.Web;
web.AllowUnsafeUpdates = true;
string contentType = "";
 
string url = http://mydomain.com/libraryname/foldername/document.doc
 
            SPFile tempFile = web.GetFile(url);
 
            byte[] obj = (byte[])tempFile.OpenBinary();
 
            switch (casestring)
            {
 
                case "txt":
 
                    contentType = "text/plain";
 
                    break;
 
                case "htm": contentType = "text/html";
 
                    break;
 
                case "html": contentType = "text/html";
 
                    break;
 
                case "rtf": contentType = "text/richtext";
 
                    break;
 
                case "jpg": contentType = "image/jpeg";
 
                    break;
 
                case "jpeg": contentType = "image/jpeg";
 
                    break;
 
                case "gif": contentType = "image/gif";
 
                    break;
 
                case "bmp": contentType = "image/bmp";
 
                    break;
 
                case "mpg": contentType = "video/mpeg";
 
                    break;
 
                case "mpeg": contentType = "video/mpeg";
 
                    break;
 
                case "avi": contentType = "video/avi";
 
                    break;
 
                case "pdf": contentType = "application/pdf";
 
                    break;
 
                case "doc": contentType = "application/msword";
 
                    break;
 
                case "dot": contentType = "application/msword";
 
                    break;
 
                case "csv": contentType = "application/vnd.msexcel";
 
                    break;
 
                case ".xls": contentType = "application/vnd.msexcel";
 
                    break;
 
                case ".xlt": contentType = "application/vnd.msexcel";
 
                    break;
 
                default: contentType = "application/octet-stream";
 
                    break;
 
            }
 
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename= " + DocName);
            HttpContext.Current.Response.ContentType = contentType;
 
            //Check that the client is connected and has not closed the connection after the request
 
            if (HttpContext.Current.Response.IsClientConnected)
                HttpContext.Current.Response.BinaryWrite(obj);
 
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.Close();

Open in new window

0
Comment
Question by:darenceang
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 17

Expert Comment

by:GreatGerm
ID: 24361420
I guess I'm curious as to why you aren't using the OOB document library interface?
0
 
LVL 4

Author Comment

by:darenceang
ID: 24361681
Hi GreatGerm!

What u mean by OOB document library interface?
Sorry... noob
:(
0
 
LVL 17

Expert Comment

by:GreatGerm
ID: 24363802
SharePoint has a decent interface in its document library list type that handles what you're wanting to do.  You can either just use it in the list or use their script to handle it at runtime.  You can see how it's setup by looking at any document library, but I have also included the page code that it uses.
 
<TR class="ms-alternating">
	<TD Class="ms-vb-icon">
		<A TABINDEX=-1 HREF="/sites/sitename/doclibrary/fileName.aspx" onclick="return DispEx(this,event,'TRUE','FALSE','FALSE','','0','SharePoint.OpenDocuments','SharePoint.WebPartPage.Document','1','','324','0','0','0x7fffffffffffffff')">
			<IMG BORDER=0 ALT="fileName.aspx" title="fileName.aspx" SRC="/_layouts/images/icsmrtpg.gif">
		</A>
	</TD>
	<TD Class="ms-vb-title" height="100%">
		<table height="100%" cellspacing=0 class="ms-unselectedtitle" onmouseover="OnItem(this)" CTXName="ctx1" Id="5" Url="/sites/sitename/doclibrary/fileName.aspx" DRef="sites/sitename/doclibrary" Perm="0x7fffffffffffffff" Type="SharePoint.WebPartPage.Document" Ext="aspx" Icon="icsmrtpg.gif|Microsoft Office SharePoint Designer|SharePoint.OpenDocuments" OType="0" COUId="" SRed="1" COut="0" HCD="" CSrc="" MS="0" CType="Document" CId="0x010100626BBFC9A12B5D40966CF75650AD5A0E" UIS="512" SUrl="">
			<tr>
				<td width="100%" Class="ms-vb">
					<A onfocus="OnLink(this)" HREF="/sites/sitename/doclibrary/fileName.aspx" onclick="return DispEx(this,event,'TRUE','FALSE','FALSE','','0','SharePoint.OpenDocuments','SharePoint.WebPartPage.Document','1','','324','0','0','0x7fffffffffffffff')">
						fileName
						<img src="/_layouts/images/blank.gif" class="ms-hidden" border=0 width=1 height=1 alt="Use SHIFT+ENTER to open the menu (new window).">
					</A>
				</td>
				<td>
					<img src="/_layouts/images/blank.gif" width=13 style="visibility:hidden" alt="">
				</td>
			</tr>
		</table>
	</TD>
	<TD Class="ms-vb2">
		<NOBR>12/23/2008 11:03 AM</NOBR>
	</TD>
	<TD Class="ms-vb-user">
		<table cellpadding=0 cellspacing=0 dir="">
			<tr>
				<td style="padding-right: 3px;">
					<a href="javascript:" onclick="IMNImageOnClick();return false;" class="ms-imnlink">
						<img title="" alt="No presence information" name="imnmark" border="0" valign="middle" height="12" width="12" src="/_layouts/images/blank.gif" sip="USER" id="imn2,type=sip" >
					</a>
				</td>
				<td style="padding: 1px 0px 0px 0px;" class="ms-vb">
					<A ONCLICK="GoToLink(this);return false;" HREF="/sites/sitename/_layouts/userdisp.aspx?ID=1">
						USER
					</A>
				</td>
			</tr>
		</table>
	</TD>
</TR>

Open in new window

0
 
LVL 4

Accepted Solution

by:
darenceang earned 0 total points
ID: 24372168
Solved!
The change is Response.End instead of Response.close

This will allow the buffer to finish what it does before closing.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

636 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