Upload mutliple files using FileUpload asp.net - asp.net

I have been struggling with this issue for some time now.
I have a web interface where I create projects, with this you should be able to upload multiple documents to the server, which is attached to the project they belong to.
So far I made it work with only uploading one file at a time.
My idea was maybe to add the "file" objects in an arralylist and then bind it to a gridview and then upload it to the server?
I know this sound a bit confusing, but I'm confused also!
This is how my method look like when I upload a single file to the server and also save the file's ID, name and context in the database:
File file = new File();
Projects_Overview po = new Projects_Overview();
po.Name = TextBoxName.Text;
po.Time = time;
po.Owner = TextBoxOwner.Text;
po.Responsibility = TextBoxResp.Text;
po.Created = datecreate;
po.StartDate = datestart;
po.FinishDate = datefinish;
po.ETC = dateETC;
po.Description = TextBoxDesc.Text;
po.Comments = TextBoxComments.Text;
po.Remember = TextBoxRemember.Text;
ie.Projects_Overview.AddObject(po);
ie.SaveChanges();
if (uploadFile.HasFile)
{
string filepath = uploadFile.PostedFile.FileName;
string fileName = Path.GetFileName(filepath);
file.Context = Path.GetFileNameWithoutExtension(uploadFile.PostedFile.FileName);
file.Extension = Path.GetExtension(uploadFile.PostedFile.FileName).Replace(".", "").Substring(0, 4);
file.FileName = Guid.NewGuid();
string fileSavePath = Server.MapPath("") + "\\Wordfiles\\" + Convert.ToString(file.FileName) + ".upl";
uploadFile.PostedFile.SaveAs(fileSavePath);
file.ProjectID = po.ID;
ie.File.AddObject(file);
ie.SaveChanges();
Can someone tell me how to "attach" multiple files and then save them in the folder along with the other data as I posted above?
Cheers

I believe that HTML 5 has the built-in capability to handle multi-file uploads (where HTML4 does not), but I have not worked with HTML5 yet. With HTML4, you must use an external control (typically either javascript or flash), as there is no built-in mechanism to allow multi-file uploads.
I did write a multi file upload page that works with HTML4 or HTML5 utilizing PLupload (an external control) to perform the file picking. I had to find a generic file handler to interface the control to the .aspx upload page. I think that I found that here.
It took a couple hours to implement, but it's worked great.

Related

How to pull captions from user-specific Resource Files?

I have two Resource files under the app_GlobalResources folder in my Website project, (CaptionsA.resx and CaptionsB.resx), for CustomerA and CustomerB,respectively.
For example, in CaptionsA.resx, I have:
MyButtonText ------> Click me!
And in CaptionsB.resx, I have:
MyButtonText ------> Click Here
I have to use captions on multiple pages in my Website. But, when CustomerA uses the website all the captions from CaptionsA.resx should be visible and when CustomerB uses the website all the captions from CaptionsB.resx should be visible. Keep in mind that both customers use English as the website language, So I can't use the culture/language localization thingy.
What I want to ask is:
How to programmatically tell my website which Resource file to use when?
What to write in my VB.net code?
How to access the Resource File in my code?
If CustomerType = CustomerA
//RETRIEVE DATA FROM CaptionsA.resx (How to do this?)
else If CustomerType = CustomerB
//RETRIEVE DATA FROM CaptionsB.resx (How to do this?)
And what shall I write in the aspx source file?
<asp:Label ID="LblButtonText" runat="server" Text="<%$ Resources:**WHAT-TO-WRITE-HERE?**,MyButtonText %>"></asp:Label>
I have been searching a lot and have tried to find the answer on a gazillion forums, but threads related to this topic were mostly unanswered or were not helpful.
here is how you do it..
Dim resourceFileBaseName As String = "WebApplicationNamespace.app_GlobalResources.CaptionsA"
Dim isCustomerB As Boolean = True
If isCustomerB Then
resourceFileBaseName = "WebApplicationNamespace.app_GlobalResources.CaptionsB"
End If
Dim customerBasedResourceManager = New System.Resources.ResourceManager(resourceFileBaseName, GetType(CaptionsA).Assembly)
Dim resourceManagerField = GetType(CaptionsA).GetField("resourceMan", BindingFlags.[Static] Or BindingFlags.NonPublic)
resourceManagerField.SetValue(Nothing, customerBasedResourceManager)
All ResX files generate an equivalent class (e.g. CaptionsA) which have an underlying ResourceManager which points to the CaptionsA resource containing all the strings. Based on the customer type, we can make this resource manager point to the right underlying resx file. but this Resource Manager is internal to the class, hence we need to reflect and set the value. Also, the CaptionsA and CaptionsB have no relation to each other, otherwise we could have leveraged some pattern/casting to access their members.
what we're doing in the above code is:
set the right resource file base name based on the customer type. (ensure you're using the right namespace path for the classes)
create a custom resourcemanager which points to our actual resx file.
set the CaptionsA class' resourcemanager to our custom one by reflection.
now whenever you try to access a resource, based on the underlying resx it'll access captionsA.resx or CaptionsB.resx.
one thing you'll notice is that you'll be accessing resources of CaptionsB.resx too via CaptionsA class. this is unavoidable and is the closest to the culture based seamless resource access we can get via non-culture based varying resources.
for the fun of it, here is the C# code as well.
string resourceFileBaseName = "WebApplicationNamespace.app_GlobalResources.CaptionsA";
bool isCustomerB = true;
if (isCustomerB)
{
resourceFileBaseName = "WebApplicationNamespace.app_GlobalResources.CaptionsB";
}
var customerBasedResourceManager = new System.Resources.ResourceManager(resourceFileBaseName,
typeof(CaptionsA).Assembly);
var resourceManagerField = typeof(CaptionsA).GetField("resourceMan", BindingFlags.Static | BindingFlags.NonPublic);
resourceManagerField.SetValue(null, customerBasedResourceManager);
CaptionsA.MyButtonText will point to the value based on the customer type's resx file.

Is it possible to read data from an excel file without uploading it to the server in VB.Net?

I need to be able to read the data from an excel file and upload the data to a database after validating it.
However, the server I'm working with does not allow write privileges for the web applications, so I need to know if it is possible to read from an excel file without writing it to the server through upload?
So far I haven't been able to find a clear answer.
Thanks!
Let's say your upload control is called fileUpload.
You don't need to do a fileUpload.SaveAs("path"). You can read the stream with fileUpload.PostedFile.InputStream. I used this for a zip file with excel sheets in it (the library is Ionic by the way):
using (var file = ZipFile.Read(fileUpload.PostedFile.InputStream))
{
foreach (var zipEntry in file.Where(ze => ze.FileName.EndsWith(".xls")
|| ze.FileName.EndsWith(".xlsx")))
{
// process the Excel files here.
}
}
Sorry, I'm not very familiar with VB.net, so the following might be wrong. But because you asked for a VB.net version:
Using file As var = ZipFile.Read(fileUpload.PostedFile.InputStream)
For Each zipEntry As var In file.Where(ze => ze.FileName.EndsWith(".xls") or ze.FileName.EndsWith(".xlsx"))
' process the Excel files here.
Next
End Using

ASP.NET creating resources at runtime

I'm developing an ASP.NET webapp that has a multilanguage feature allowing the webmaster to create new languages at runtime.
The approach that I was thinking is the following:
The user selects one available (not created) language.
When the user confirms, the application automatically copies a set of existing resources, replacing the filename with the new culture. For example: default.aspx.en-us.resx to default.aspx.es-ar.resx.
The user edits the recently created resources.
Currently I'm having troubles with step number 2. I've achieved to copy the resources, but then these new resources are ignored. I think that this happens because the new resources are not included in the running assembly, and therefore are being ignored.
When I test the following code in my local project, I would have to manually add the new resources to the solution and then recompile to make it work.
Does anyone know how to make this work?
This is the code of the mentioned copy.
string _dir = path_ + "App_LocalResources\\\\";
DirectoryInfo _dirInfo = new DirectoryInfo(_dir);
foreach (FileInfo _file in _dirInfo.GetFiles("*en-us.resx")) {
_file.CopyTo(_dir + _file.Name.Replace("en-us", idioma_.Cultura));
}
string _dir2 = path_ + "App_GlobalResources\\\\";
_dirInfo = new DirectoryInfo(_dir2);
foreach (FileInfo _file in _dirInfo.GetFiles("*en-us.resx")) {
_file.CopyTo(_dir2 + _file.Name.Replace("en-us", idioma_.Cultura));
}
Thank you very much.
Creating or editing Resource files is not possible the same way as reading data.
In order to create or edit a resource file, you should do it the same way you create or edit XML files because resource files have with a specific structured XML elements.
Maybe this article will help you...

Scripting.FileSystemObject location (from JavaScript)

I have a file for my Windows sidebar gadget that stores some settings for the user, but the FSO object seems to pick its native residence as the desktop, meaning that if I don't specify a directory, it will put this file on the desktop. I would specify the whole location, but I want to be able to put this on other people's computers without having stuff on their desktop or elsewhere besides the gadget folder.
I know this is possible in XMLHttpRequest, but I've had trouble with that in the past, and it would be better if I could just avoid it altogether, if possible.
function writeSetting(text)
{
var fil = new ActiveXObject("Scripting.FileSystemObject");
var writer = fil.OpenTextFile("loc.txt", 2, true);
writer.WriteLine(text);
writer.Close();
}
Use the System.Gadget.path property to get the gadget's path and append to it as needed. See the example in the link.
Happy coding.

Saving a file in Flex Air

I'm trying to copy my SQLite file that is used in my Air app to user's selected directory using
var fileSaveDest:FileReference = new FileReference();
fileSaveDest.save(dbWorkedFile,'Inventory.DB');
dbWorkedFile is a File
dbWorkedFile = File.documentsDirectory.resolvePath("Inventory.db");
I tried this but the saved file isn't a valid SQLite file.
Also, I was wondering whether it's possible to embed SQLite to Air? If so how can I import and export the database?
Many thanks
In the end I couldn't get FileReference.save() to work so I go with the regular File's browseForSave()
dbWorkedFile.addEventListener(Event.SELECT, savingDatabase);
dbWorkedFile.browseForSave('Specify save location');
private function savingDatabase(event:Event):void
{
var selectedFile:File = File(event.target);
//To ensure the file is still loaded
dbWorkedFile = File.applicationStorageDirectory.resolvePath("Inventory.db");
dbWorkedFile.copyTo(selectedFile, true);
}
There is a short article describing how to include some SQLLite files in an AIR application on Adobe website (cookbooks section).

Resources