PDA

View Full Version : [ASP.Net] Upload .csv to SQL Server


mfrias
07-27-2010, 11:44 AM
Hi,

I am building a web service in ASP.NET to allow users to upload a .csv file into SQL Server via a Flex interface.

So far I have the following function in Flex that fires up when the user selects a file:

private function uploadFile(event:MouseEvent):void
{
if (!fileSelected)
Alert.show("Select a file!");
else
fileRef.upload(new URLRequest("webserviceFunctionAddress"));
}

And in ASP.NET I have this Web function that for the moment stores the file in the server:

<WebMethod()> _
Public Function importFile(ByVal f As Byte(), ByVal fileName As String) As String
Try
Dim ms As New MemoryStream(f)
Dim fs As New FileStream(System.Web.Hosting.HostingEnvironment.M apPath("C:temp") & fileName, FileMode.Create)
ms.WriteTo(fs)
ms.Close()
fs.Close()
fs.Dispose()
Return "ok"

Catch ex As Exception
Return ex.Message.ToString()
End Try

End Function

When I execute the Flex app I always get the error 2038 and httpFox says that no request is made to the server.

The web service function is asking for two values but even if I delete the filename as a parameter the error persists.

There must be something wrong or maybe I have a totally wrong approach. Any ideas, links or suggestions are appreciated.

Manolo

mfrias
07-30-2010, 10:37 AM
Ok, I have now redone the ASP script. I created an ASP Web app instead of a web service:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim uploadFile As HttpPostedFile = Request.Files("file")
uploadFile.SaveAs("test")

End Sub

The question is how do I do with the Flex code? I am constantly getting the 2038 i/o error with this code:

<mx:Canvas width="100%" height="100%" backgroundColor="#91A7B2">
<mx:Script>
<![CDATA[
import mx.managers.PopUpManager;
import mx.controls.Alert;

[Bindable]
private var fileRef:FileReference = new FileReference();
[Bindable]
private var fileSelected:Boolean = false;
[Bindable]
private var statusFile:String = "Select file";

private var urlReq:URLRequest = new URLRequest();

private function onCreationComplete():void
{
fileRef = new FileReference();
fileRef.addEventListener(Event.SELECT, onFileSelected);
fileRef.addEventListener(Event.COMPLETE, onUploadComplete);
fileRef.addEventListener(ProgressEvent.PROGRESS, onUploadProgress);
fileRef.addEventListener(IOErrorEvent.IO_ERROR, onUploadError);
fileRef.addEventListener(SecurityErrorEvent.SECURI TY_ERROR, onUploadError);
}

private function closeMe():void
{
PopUpManager.removePopUp(this);
}
private function selectFile(evt:MouseEvent):void
{
fileRef.browse();
}
private function onFileSelected(evt:Event):void
{
fileSelected = true;
fileInput.text = fileRef.name;
statusFile = "Ready";
}
private function uploadFile(event:MouseEvent):void
{
if (!fileSelected)
Alert.show("Select a file!");
else
urlReq.url = "webAppAdress/Default.aspx";
fileRef.upload(urlReq,"test");
}
private function onUploadProgress(event:ProgressEvent):void
{
statusFile = ((event.bytesLoaded * 100 / event.bytesTotal).toString());
}
private function onUploadComplete(event:DataEvent):void
{
statusFile = "Complete";
}
private function onUploadError(event:Event):void
{
if (event is IOErrorEvent)
{
Alert.show((event as IOErrorEvent).toString());
}
else if (event is SecurityErrorEvent)
{
Alert.show((event as SecurityErrorEvent).toString());
}
}

]]>
</mx:Script>

<mx:VBox x="52" y="70">
<mx:HBox>
<mx:Button label="Browse" click="selectFile(event)"/>
<mx:TextInput id="fileInput" editable="false" width="200"/>
</mx:HBox>
<mx:Button label="Upload file" click="uploadFile(event)" enabled="{fileSelected}"/>
<mx:HBox>
<mx:Text text="Status: "/>
<mx:Text text="{statusFile}"/>
</mx:HBox>
</mx:VBox>
</mx:Canvas>

Any ideas will be appreciated.
Manolo