How to upload files (stored on SQL) to web server?

Hi Experts,

I got files stored on SQL server db. I would like to be able to upload them onto web server.

I know how to retrieve th! em, but not sure how to upload them to web server. Please help. Thanks so much.

Below are codes for retrieving file info from sql server:

Dim strFileName As String = Quote.GetQuoteFileByQuoteFileID(CInt(selectedQuoteFileID)).Tables(0).Rows(0).Item("QuoteFileName")
Dim strFileType As String = Quote.GetQuoteFileByQuoteFileID(CInt(selectedQuoteFileID)).Tables(0).Rows(0).Item("QuoteFileType")
Dim strFileSize As Integer = Quote.GetQuoteFileByQuoteFileID(CInt(selectedQuoteFileID)).Tables(0).Rows(0).Item("QuoteFileSize")
Dim strFileData As Byte() = Quote.GetQuoteFileByQuoteFileID(CInt(selectedQuoteFileID)).Tables(0).Rows(0).Item("QuoteFileData")

I tried this, but got "Object reference not set to an instance of an object." error.

Dim sSavePath = "~/TempEmailAttachment/"

Dim myFile As HttpPostedFile

Dim myData() As Byte = New Byte(strFileSize) {}

myFile.InputStream.Read(myData, 0, strFileSize)

Dim newFile As System.IO.FileStream = New System.IO.FileStream(Server.MapPath(sSavePath + strFileName), System.IO.FileMode.Create)

newFile.Write(myData, 0, myData.Length)

newFile.Close()


 

0
uuthanh
4/28/2005 7:26:45 PM
asp.net.getting-started 91979 articles. 4 followers. Follow

3 Replies
798 Views

Similar Articles

[PageSpeed] 19
Get it on Google Play
Get it on Apple App Store

 uuthanh wrote:
I tried this, but got "Object reference not set to an instance of an object." error.

Getting this error usually means you forgot the New keyword somewhere. Do you also know where, on which line, the error occurs?

Also take a look at this article: Uploading In ASP.NET

Grz, Kris.


Read my blog. Handy Firefox plugins for web developers.


Workaround for non working Mark as answer buttons.
0
XIII
4/28/2005 8:43:20 PM

it does not explicitly tell which line contains errors. Here's my error page.

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
   SalesOpp.ConvertToOrderGO.btnSaveFile_Click(Object sender, EventArgs e) +629
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
   System.Web.UI.Page.ProcessRequestMain() +1292

 

0
uuthanh
4/28/2005 8:53:03 PM

Few days ago I had written a function for uploading files to webserver.In this code I had also checked the filetypes and dimensions of file to be uploaded.As I had to allow only images thats y I had type the types that images usually have and alo checked the height width of it.U can modify it according to ur requirement and it wirks fine as I am using it.

private void UploadFile()

{

//File1 is name of HTML  File control used to browse and select file

if (File1.PostedFile !=null && File1.Value!=string.Empty) //Checking for valid file

{

string StrFileName = File1.PostedFile.FileName.Substring(File1.PostedFile.FileName.LastIndexOf("\\") + 1) ;

string StrFileType = File1.PostedFile.ContentType.ToLower();

if(StrFileType!="image/bmp" && StrFileType!="image/gif" && StrFileType!="image/pjpeg" && StrFileType!="image/jpeg")

{

this.strError = "File format not supported.Please select image of type bmp,jpg or gif!";

}

else

{

//Create an image object from the uploaded file

System.Drawing.Image UploadedImage = System.Drawing.Image.FromStream(File1.PostedFile.InputStream);

//Determine width and height of uploaded image

float UploadedImageWidth = UploadedImage.PhysicalDimension.Width;

float UploadedImageHeight = UploadedImage.PhysicalDimension.Height;

//Check that image does not exceed maximum dimension settings

if (UploadedImageWidth > 551 || UploadedImageHeight > 112)

{

this.strError = "This image is too big - please resize it!";

}

else

{

int IntFileSize =File1.PostedFile.ContentLength;

if (IntFileSize >=0)

{

string pathstore = "../Business/images/"+StrFileName;

File1.PostedFile.SaveAs(Server.MapPath(pathstore));

}

}

}

}

}

It works weel.Feel free to asl more...

0
littlescholar
4/29/2005 9:20:12 AM
Reply:

Similar Artilces:

how to save file (stored on SQL server) to file/web server?
Hi Experts, I got files stored on SQL server db. I would like to be able to save them onto file/web server. I know how to retrieve them, but not sure how to save to file/web server. Please help. Thanks so much. Below are codes for retrieving file info from sql server:Dim strFileName As String = Quote.GetQuoteFileByQuoteFileID(CInt(selectedQuoteFileID)).Tables(0).Rows(0).Item("QuoteFileName")Dim strFileType As String = Quote.GetQuoteFileByQuoteFileID(CInt(selectedQuoteFileID)).Tables(0).Rows(0).Item("QuoteFileType")Dim strFileSize As Integer = Quote.GetQuoteFileByQuoteFileID(CInt(selectedQ...

Store uploaded files on web server or in SQL?
Curious as to which way you think is the best.  From ease of coding, it's simplier to file on the server it seems than to store in sql.  So, if you prefer sql, do you have a sample you can send me?  Never make important decisions on a Monday! http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=414Actually, I prefer the filesystem.  Regards Mike [MVP - ASP/ASP.NET]My site Hi, Look at the following code which i have to stored images in windows application(not web) in the sql server but this is not the best practice to store images. If you are using docu...

How to upload or copy file from web server to remote sql server
  Hi All,   q. How to upload or copy file from web server to remote server..?.  I want to uploaded excel file from client machine to remote sql server via web server.   i did 1st part and i dont know how to send file from wbe server to remote database server  pls give me solution or usefull links iif any one knows how to do this ..   Thanks in advance.     Are you saying you want to send the file there, or extract the spreadsheet data and put it into a database table?    If the former, I suggest setti...

Uploading files to a different file server but not web server
Hi, I want to upload files to a different file server than the web server using asp.net, is that possible?I can use Server.MapPath to store it on the same server. How do I save it to a different machine? What all I need? Do I need IP address? Do I need to have a shared folder on that machine? thank u for the help!!! There is only a few built in ways to get a file from one machine to another in windows.  Things like FTP, HTTP, or File Sharing - you are going to need a path to the server.  You cannot upload directly to that server.  You'll need to upload to your web s...

File Upload to server and save path to uploaded file into a sql database
   I am trying to upload a file to a server and save the path of the uploaded file to a sql 2005 server. I have some code below but im getting the error Object reference not set to an instance of an object. Any help would be greatful. By the way I got this code from these forums and just tweaked them for my website. 1 Dim Fileupload1 As FileUpload = CType(FindControl("FileUpload1"), FileUpload) 2 Dim filePath1 As Label = CType(FindControl("filePath1"), Label) 3 4 If Fileupload1.HasFile Then 5 Try 6 Fileupload1...

Store and get flash files from SQL Server
Hi Everyone, I want to store and get flash files(.swf extension) from sql server. I tried using the image datatype in sql server which stores binary data. Is this correct? Also, how can i get the file out from sql server? Thanks alot, HenryToronto Trade show displays | Toronto Printing Solutions | Toronto Printing Yes. Check out this article on retrieving images from SQL Server (just replace image-specific stuff, which is NOT very much, with flash-specific stuff). http://www.dotnetbips.com/displayarticle.aspx?id=60Darrell Norton, MVPDarrell Norton's BlogPlease mark this ...

Get URL for a file uploaded to the web-server
Hi I'm wondering if there's an easy way to get the URL for a file uploaded with FileUpload control? I can get the physical path from the webserver by using Server.MapPathThe problem is that this is the local path for the file i get then. Is there a similar way to get the URL instead of the local path from the server? I have a solution, but i don't think it's smart. The way I'm doing it now is by splitting the path into pieces (seperator = \) and write: 'http://servername:port/ + @"/" + strPiece1 + @"/" + strPiece2' and so on. Thanks, Thoma...

Get SourceSafe files and upload to the web server
How can I know where VSS is storing the checked in versions of a web site to upload them to the web server? Thanks.Juliano NunesMCAD | MCP | Microsoft Student Partnerhttp://julianonetfox2.spaces.live.com...

I created a website on my localhost with three .mdf files using SQL express but now I get an error when I try to host it on my web server without SQL express. need some help on how to change my connec
An error has occurred while establishing a connection to the server.  When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlClient.SqlException: An error has occurred whil...

How to get Excel or PDF file stored into SQL server ?
I've created a table in SQL server to store/retrieve binary file, I used image as field type to store that binary and add another filed to store the image type, I saved image file, excel, pdf, word, etc. successfully in an ASP.NET environment, and I can retrieved the image files by add simply two lines as below: Response.ContentType = myDataReader.Item("myImgTyp")Response.BinaryWrite(myDataReader.Item("myImgField")) How, I cannot apply the same way to retrieve file of other type, also whenever I retrieved and save, I cannot handle the file extension as the same file type! If anyone ...

How do setup a directory on a web server to store upload files
I'm trying to store upload images on my webserver but don't know how to create the directory to save the file. Would someone kind enough to show me an example on  how to create the folder Thanks star36 Below is a sample code string storePath = Server.MapPath("~") + "UploadFiles"; if(!System.IO.Directory.Exists(storePath))System.IO.Directory.CreateDirectory(storePath); HCHaissam Abdul MalakMCAD.NET| Blog | ok i saw your link and i tryed to use the method but i got an error on Path.GetFileName. thanks The Path class in under the System.IO namespace. ...

File upload to a server other than the web server.
I have a very simple asp.net upload page. Everything works great when saving the file locally (web server). Things are getting ugly when trying to save files to a different machine (file server, database server,...) So, when using .SaveAs and passing in the UNC path to the new server location I get an *Access denied* error messgae. When mapping a network drive and using the drive letter in the .SaveAs path I get an message saying parts of the path cannot be found. any ideas? thanks  --tolgaApplication Developer Make sure that the account running asp.net web applicaiton(defa...

Urgent! Upload text file to SQL Server in VB.NET
Hi Experts, I am working on an HR Web application which should upload applicant's resume to our SQL Server 2000, a BLOB column.However, it inserts without error, but the resume isn't stored in table at all. The table structure is:CREATE TABLE [dbo].[Mgmt_App_Resume_Table] ( [Applicant_ID] [smallint] NULL , [App_Resume] [image] NULL  ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]After the program executed, the Applicant_ID is inserted fine, but the App_Resume column is empty.  The file I uploaded is a MS word doc. and it's a resume.....Where did I do wrong?  Ple...

Getting data form SQL Server 2005 to a Web.SiteMap file
I want read a hierarchal data table in SQL Server 2005 to write My Web.SiteMap file. How I to do it? web.sitemap is a static file used by the XmlSiteMapProvider if you know your site navigation at development time. If you have a data-driven navigation, then you should consider creating your own SiteMapProvider. -Brock  DevelopMentorhttp://staff.develop.com/ballen...

Web resources about - How to upload files (stored on SQL) to web server? - asp.net.getting-started

Resources last updated: 11/23/2015 4:03:31 AM